我们如何比较两个MySQL表中的数据?
有时我们需要从两个表中识别出不匹配的数据,尤其是在数据迁移的情况下。可以通过比较表格来完成。考虑下面的示例,其中有两个名为“ students”和“ student1”的表。
mysql> Select * from students;+--------+--------+----------+
| RollNo | Name | Subject |
+--------+--------+----------+
| 100 | Gaurav | Computer |
| 101 | Raman | History |
| 102 | Somil | Computer |
+--------+--------+----------+
3 rows in set (0.00 sec)
mysql> select * from student1;
+--------+--------+----------+
| RollNo | Name | Subject |
+--------+--------+----------+
| 100 | Gaurav | Computer |
| 101 | Raman | History |
| 102 | Somil | Computer |
| 103 | Rahul | DBMS |
| 104 | Aarav | History |
+--------+--------+----------+
5 rows in set (0.00 sec)
现在,借助下面的查询,我们可以比较这些表并获取不匹配的行作为结果集。
mysql> Select RollNo,Name,Subject from(select RollNo,Name,Subject from students union all select RollNo,Name,Subject from Student1)as std GROUP BY RollNo,Name,Subject HAVING Count(*) = 1 ORDER BY RollNo;+--------+-------+---------+
| RollNo | Name | Subject |
+--------+-------+---------+
| 103 | Rahul | DBMS |
| 104 | Aarav | History |
+--------+-------+---------+
1 rows in set (0.02 sec)
以上是 我们如何比较两个MySQL表中的数据? 的全部内容, 来源链接: utcz.com/z/331242.html