首页  >  文章  >  数据库  >  我们如何比较两个 MySQL 表中的数据?

我们如何比较两个 MySQL 表中的数据?

WBOY
WBOY转载
2023-09-08 12:45:02755浏览

我们如何比较两个 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 表中的数据?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除