我有2张这样的桌子:
表Student_Old:
id name city
1 A X
2 B Y
3 C Z
表Student_new:
id name city
1 A X
2 M Y
3 C K
如您所见,对于ID 2,名称在ID中不匹配,对于ID 3,城市在两个表中都不匹配(我正在对ID进行比较,ID是两个表中的主键,基本上student_new是old的备份表)。现在,我想获取这两个不匹配的行。 例如: student_old s1,student_new s2-
s1.id s2.id s1.name s2.name s1.city s2.city
2 2 B M X X
3 3 C C Z K
答案 0 :(得分:0)
使用内部联接,where子句会过滤掉不匹配的名称和城市
select a.* , b.* from Student_Old inner join Student_new
on a.id=b.id
where a.name<>b.name or a.city<>b.city
答案 1 :(得分:0)
使用加入
select os.*,ns.* Student_Old os join Student_new ns on os.id=ns.id
where os.city!=ns.city OR os.name!=ns.name
答案 2 :(得分:0)
SELECT so.id AS id_1, so.name AS name_1, so.city AS city_1,
sn.id AS id_2, sn.name AS name_2, sn.city AS city_2
FROM student_old so
INNER JOIN student_new sn
ON so.id = sn.id
WHERE so.name <> sn.name AND so.city <> sn.city
答案 3 :(得分:0)
由于您不在乎一个表中的记录,而不在乎另一个表中的记录,因此简单的JOIN就足够了:
SELECT s1.id AS old_id, s2.id AS new_id,
s1.name AS old_name, s2.name AS new_name,
s1.city AS old_city, s2.city AS new_city
FROM student_old s1
JOIN student_new s2
ON s1.id = s2.id
WHERE s1.name != s2.name OR s1.city != s2.city
输出:
old_id new_id old_name new_name old_city new_city
2 2 B M Y Y
3 3 C C Z K