MYSQL中同一个数据库中的两个表中的数据怎样合并?
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)
展开全部
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; )
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; )
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询