MYSQL中同一个数据库中的两个表中的数据怎样合并?

 我来答
zhangqiaoqiaox
推荐于2019-11-01 · TA获得超过3万个赞
知道小有建树答主
回答量:31
采纳率:100%
帮助的人:2万
展开全部

1、忽略表之间的关联关系
ALTER TABLE db2.dbo.table NOCHECK CONSTRAINT 关系名

2、--将没有重复的数据合并

insert into db2.dbo.table(field1,field2...) select field1,field2... from db1.dbo.table a where a.username not in (select username from db2.dbo.table)

3、将重复的数据写入临时表
select field1,field2... into 新的临时表 from db1.dbo.table a where a.username in (select username from db2.dbo.table) 

百度网友c76e578
2018-01-31 · TA获得超过283个赞
知道小有建树答主
回答量:92
采纳率:0%
帮助的人:87.5万
展开全部
1.直接把结果更新在aaa表中的语句如下
update aaa
set post = (select sum_post from (select aaa.ID,(aaa.post+bbb.post) sum_post from aaa,bbb where aaa.ID=bbb.ID) t1 where t1.ID=a.ID)
where exists (select 1 from bbb where aaa.ID =bbb.ID);

2.直接查询显示的方法参见上楼;

3.新建ccc表:
create table ccc as( select id,username,sum(post) sum_post from
(select id,username,post from aaa
union all
select id,username,post from bbb)
group by id,username; )
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式