SQL语句中 LEFT JOIN 后 ON 和 WHERE 的区别
阐述
写SQL时本想通过 A left B join on and
后面的条件查出的两条记录变成一条,奈何发现还是有两条。
后来发现 join on and
不会过滤结果记录条数,只会根据 and 后的条件是否显示 B
表的记录,A
表的记录一定会显示。
不管 and 后面的是 A.id=1
还是 B.id=1
,都显示出 A
表中所有的记录,并关联显示 B
中对应 A
表中 id
为 1
的记录或者B表中 id 为 1 的记录。
SELECT u.id,u.nickname,c.content FROM chat_user u
LEFT JOIN chat_communication c
on u.id=c.toid order by c.id
+----+------------------+--------------+
| id | nickname | content |
+----+------------------+--------------+
| 86 | 大美如斯 | NULL |
| 88 | 悦悦 | NULL |
| 89 | 雨薇 | NULL |
| 87 | 大金 | 你好 |
| 85 | Love violet life | 你也好 |
| 87 | 大金 | 你在干嘛 |
| 87 | 大金 | 你还在吗 |
| 87 | 大金 | 订单 |
+----+------------------+--------------+
8 rows in set (0.00 sec)
SELECT u.id,u.nickname,c.content FROM chat_user u
LEFT JOIN chat_communication c
on u.id=c.toid and u.nickname="大金" order by c.id
+----+------------------+--------------+
| id | nickname | content |
+----+------------------+--------------+
| 85 | Love violet life | NULL |
| 86 | 大美如斯 | NULL |
| 88 | 悦悦 | NULL |
| 89 | 雨薇 | NULL |
| 87 | 大金 | 你好 |
| 87 | 大金 | 你在干嘛 |
| 87 | 大金 | 你还在吗 |
| 87 | 大金 | 订单 |
+----+------------------+--------------+
8 rows in set (0.00 sec)
SELECT u.id,u.nickname,c.content,c.fromname FROM chat_user u
LEFT JOIN chat_communication c
on u.id=c.toid and c.fromname="雨薇" order by c.id
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
left join 的 on 和 where 的条件区别
1、 on 条件是在生成临时表时使用的条件,它不管 on 中的条件是否为真,都会返回左边表中的记录。
2、where 条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有 left join 的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
新建两张表:
CREATE TABLE test1(id int,size int);
INSERT INTO test1 VALUES (1,10),(2,20),(3,30);
CREATE TABLE test2(size int,nikname varchar(10));
INSERT INTO test2 VALUES (10,"AAA"),(20,"BBB"),(20,"CCC");
表1:test1
mysql> select * from test1;
+------+------+
| id | size |
+------+------+
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
+------+------+
3 rows in set (0.00 sec)
表2:test2
+------+---------+
| size | nikname |
+------+---------+
| 10 | AAA |
| 20 | BBB |
| 20 | CCC |
+------+---------+
3 rows in set (0.01 sec)
第一条 SQL
SELECT * FROM test1
LEFT JOIN test2 ON (test1.size = test2.size)
WHERE test2.nikname = "AAA";
+------+------+------+---------+
| id | size | size | nikname |
+------+------+------+---------+
| 1 | 10 | 10 | AAA |
+------+------+------+---------+
1 row in set (0.00 sec)
第二条 SQL
select * FROM test1 left join test2
on (test1.size = test2.size and test2.nikname="AAA");
+------+------+------+---------+
| id | size | size | nikname |
+------+------+------+---------+
| 1 | 10 | 10 | AAA |
| 2 | 20 | NULL | NULL |
| 3 | 30 | NULL | NULL |
+------+------+------+---------+
3 rows in set (0.00 sec)
第一条SQL的过程
1、中间表 on 条件 : test1.size = test2.size
+------+------+------+---------+
| id | size | size | nikname |
+------+------+------+---------+
| 1 | 10 | 10 | AAA |
| 2 | 20 | 20 | BBB |
| 2 | 20 | 20 | CCC |
| 3 | 30 | NULL | NULL |
+------+------+------+---------+
4 rows in set (0.00 sec)
2、再对中间表过滤 where 条件:test2.nikname="AAA"
+------+------+------+---------+
| id | size | size | nikname |
+------+------+------+---------+
| 1 | 10 | 10 | AAA |
+------+------+------+---------+
1 row in set (0.00 sec)
第二条SQL的过程
1、中间表on条件:test1.size = test2.size and test2.nikname=’AAA’
(条件不为真也会返回左表中的记录)
+------+------+------+---------+
| id | size | size | nikname |
+------+------+------+---------+
| 1 | 10 | 10 | AAA |
| 2 | 20 | NULL | NULL |
| 3 | 30 | NULL | NULL |
+------+------+------+---------+
3 rows in set (0.00 sec)
其实以上结果的关键原因就是 left join,right join,full join
的特殊性,不管 on 上的条件是否为真都会返回 left 或 right 表中的记录。
full 则具有 left 和 right 的特性的并集。
而 inner join 没这个特殊性,则条件放在 on 中和 where 中,返回的结果集是相同的。
相关文章
- Azure 删除VHD时报错:There is currently a lease on the blob and no lease ID was specified in the request
- SQL Server - Management Studio - Client Statistics - Wait time on server replies vs Client processing time
- Error Message – The index ” is dependent on column ”.ALTER TABLE ALTER COLUMN ” failed because one or more objects access this column.
- What “Clustered Index Scan (Clustered)” means on SQL Server execution plan?
- 【BZOJ2588】Count On a Tree(主席树)
- ERROR: Timeout on the Spark engine during the broadcast join
- jQuery on()方法(函数)有哪些事件
- SQL Server 占用内存太高,查找占用内存高以及影响其性能的sql语句及解决方法
- 快速理解 Phoenix : SQL on HBASE
- remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
- vue中bus.$on事件被多次绑定
- vue中$refs、,$emit、$on
- Caused by: java.sql.SQLException: Couldn't perform the operation getAutoCommit: You can't perform any operations on this connection. It has been automatically closed by Proxool for some reason (see lo
- SQL join中on与where区别
- Interop: Microsoft,HP Partner On UC
- 【服务器】Web server failed to start. Port 80 or configure this application to listen on another port.
- ORA-06502: PL/SQL: 'Numeric or Value Error' When CLOB Convert to VARCHAR2 on a Multibyte Database
- PL/SQL DEVELOPER 导出表数 和 生成 执行快速 插入表的 sql
- SQL Server: Get table primary key and Foreign Key using sql query
- sql server 数据分析优化实战(一)——SQL语句优化
- sql语法:inner join on, left join on, right join on具体用法
- 怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优
- sql server使用T-SQL读取扩展事件
- (4.52)解决sql server动态SQL中对表字段操作时需要频繁 ISNULL的问题
- SQL中on条件与where条件的区别
- [CareerCup] 7.2 Ants on Polygon 多边形上的蚂蚁
- 解决CUDA error: no kernel image is available for execution on the device 测试cuda是否可用