求SQL语句:比较两张表中不一样的行
发布网友
发布时间:2023-07-23 18:59
我来回答
共3个回答
热心网友
时间:2024-10-24 12:16
答案2:
设表有三个字段,先将两个字段再加入一个同名不同值的字段联合,再对联合表按原有全部字段分组,取计数为1的记录为两表不同的记录.
select fielda,fieldb,fieldc from (select *,a='a' from tablea union select *,a='b' from tableb) a group by fielda,fieldb,fieldc having count(1)=1
===========================
答案一:
转换所有字段为字符型,合并成一个字段以方便比较,然后将两个表中独有的数据集分别取出然后连接:
SELECT * FROM A
WHERE
CONVERT(VARCHAR,FIELDA)+
CONVERT(VARCHAR,FIELDB)+
CONVERT(VARCHAR,FIELDC)
NOT IN
(SELECT
CONVERT(VARCHAR,FIELDA)+
CONVERT(VARCHAR,FIELDB)+
CONVERT(VARCHAR,FIELDC)
FROM B
)
UNION
SELECT * FROM B
WHERE
CONVERT(VARCHAR,FIELDA)+
CONVERT(VARCHAR,FIELDB)+
CONVERT(VARCHAR,FIELDC)
NOT IN
(SELECT
CONVERT(VARCHAR,FIELDA)+
CONVERT(VARCHAR,FIELDB)+
CONVERT(VARCHAR,FIELDC)
FROM A
)
热心网友
时间:2024-10-24 12:17
select * from A minus select * from B
union
select * from B minus select * from A
热心网友
时间:2024-10-24 12:17
您好,我现在也遇到这个问题了,你能不能教教我了,我是一名小小的菜鸟