《MYSQL数据库如何通过SQL找出2个表里值不同的列的方法》要点:
本文介绍了MYSQL数据库如何通过SQL找出2个表里值不同的列的方法,希望对您有用。如果有疑问,可以联系我们。
以下有两个表,他们的结构完全相同,请通过SQL找出值不同的列.MYSQL数据库
Student_1MYSQL数据库
MYSQL数据库
MYSQL数据库
Student_2MYSQL数据库
NAME |
AGE |
SCORE |
peter |
26 |
89 |
jack |
25 |
96 |
daniel |
26 |
48 |
bark |
21 |
69 |
MYSQL数据库
方法一 -- NOT EXISTS:
MYSQL数据库
代码如下:
SELECT *
FROM Student_1 S1
WHERE NOT EXISTS
(SELECT *
FROM Student_2 S2
WHERE S1.name = S2.name
AND S1.age = S2.age
AND S1.score = S2.score
)
UNION ALL
SELECT *
FROM STUDENT_2 S2
WHERE NOT EXISTS
(SELECT *
FROM STUDENT_1 S1
WHERE S1.name = S2.name
AND S1.age = S2.age
AND S1.score = S2.score
);
方法二 -- MINUS
MYSQL数据库
代码如下:
(SELECT * FROM Student_1
MINUS
SELECT * FROM Student_2)
UNION ALL
(SELECT * FROM Student_2
MINUS
SELECT * FROM Student_1)
方法三 -- HAVING GROUP BY
MYSQL数据库
代码如下:
SELECT DISTINCT name, age, score FROM (
SELECT * FROM Student_1
UNION ALL
SELECT * FROM Student_2
)GROUP BY name, age, score HAVING COUNT(*)=1 ;
转载请注明本页网址:
http://www.vephp.com/jiaocheng/3834.html