日期:2014-05-17  浏览次数:20574 次

一个sql语句的写法
1,A表、B表与C表结构相同,字段为USERID,USERNAME,SEX,AGE,STATE
如何A表中有的,B表中没有的数据插入到C表中


------解决方案--------------------
SQL code

insert C
select USERID,USERNAME,SEX,AGE,STATE
from A 
where not exists(select 1 from B 
where 
a.USERID=b.USERID and a.USERNAME=b.USERNAME 
and a.SEX=b.SEX and a.AGE=b.age and a.STATE=b.STATE
)

------解决方案--------------------
SQL code
INSERT C
SELECT userid,username,sex,age,state
FROM a
WHERE  NOT EXISTS
(SELECT 1 FROM b a.userid=userid and a.username=b.username
AND a.sex=b.sex and a.age=b.age AND a.state=b.state
)