mysql中两张表使用left join on 求差集

1.表结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mysql> select * from allStudents;
+----+-------+
| id | name  |
+----+-------+
|  1 | ????  |
|  2 | ????  |
|  3 | ???·  
|  4 | four  |
+----+-------+
4 rows in set (0.00 sec)
 
mysql> select * from currentStudents;
+----+--------+
| id | name   |
+----+--------+
|  1 | luowen |
|  3 | 毛毛想 |
+----+--------+

2.子查询方法

1
2
3
4
5
6
7
mysql> select * from test where test.id not in ( select id from user);
+----+----------+--------+
| id | name     | salary |
+----+----------+--------+
|  2 | 脙芦脙芦     |   4000 |
|  4 | four     |  23232 |
+----+----------+--------+

3.left join 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mysql> select allStudents.*,currentStudents.* from allStudents,currentStudents where allStudents.id = currentStudents.id;
+----+-------+----+---------+
| id | name  | id |    name |
+----+-------+----+---------+
|  1 | ????  |  1 | luowen  |
|  3 | ???·  |  3 | 毛毛想  |
+----+-------+----+---------+
2 rows in set (0.00 sec)
 
mysql> select allStudents.*,currentStudents.* from allStudents left join currentStudents on allStudents.id = currentStudents.id;
+----+-------+------+------------+
| id | name  | id   | name       |
+----+-------+------+------------+
|  1 | ????  |    1 | luowen     |
|  2 | ????  | NULL | NULL       |
|  3 | ???·  |    3 | 毛毛想     |
|  4 | four  | NULL | NULL       |
+----+-------++------+-----------+
4 rows in set (0.00 sec)
 
mysql> select allStudents.*,currentStudents.* from allStudents left join currentStudents on allStudents.id = currentStudents.id where currentStudents.id is null;
+----+------+------+----------+
| id | name | id   | name     |
+----+------+------+----------+
|  2 | ???? | NULL | NULL     |
|  4 | four | NULL | NULL     |
+----+------+------+----------+
2 rows in set (0.00 sec)

  

posted @   arvim  阅读(1913)  评论(0编辑  收藏  举报
编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
点击右上角即可分享
微信分享提示