日期:2014-05-18  浏览次数:20355 次

sql insert子查询问题
请问个问题!例如有两个表,表A库存表,表B进货表.现在用SQL语句比较2个表,如果表A中有的数据而表B没有那么将此数据插入表B.如果表B中有的数据而表A中没有那么将此数据插入表A!这个SQL语句怎么写

------解决方案--------------------
--如果表A中有的数据而表B没有那么将此数据插入表B.

insert into B(..)
select A....
from A left join B on A.id=B.id
whre B.id is null


--如果表B中有的数据而表A中没有那么将此数据插入表A

insert into A(..)
select B....
from B left join A on A.id=B.id
whre A.id is null

------解决方案--------------------


insert into B(字段1,字段2,字段3...)
select 字段1,字段2,字段3... from A
where ----条件
------解决方案--------------------
insert into B(字段1,字段2,字段3...)
select 字段1,字段2,字段3... from A
where not exists(select 1 from B where --条件)