当前位置:网站首页>Analysis of query results using both left join on and where in MySQL
Analysis of query results using both left join on and where in MySQL
2022-07-27 07:25:00 【hawanglc】
Used to use it before oracle, When dealing with left connections and links , Only need sql Add (+) That's all right. , But in mysql in , take left join perhaps right join And on and where When used in combination , Different combinations , The results are different . Now take a note of .
Suppose there is a left table tb_oder, The table has order_id and user_id Field ; There is a right table tb_user, The table has user_id and user_name Two fields , Two tables use user_id Association .
SQL1:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id;
This is the most common usage , If there is no corresponding data in the right table , Then the result set is empty .
SQL2:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
and u.user_id > 10;
After filtering the right table, it is associated with the left table , therefore , Even in the right table and the left table user_id<10 The data of ,user_name The display result of is also empty .
SQL3
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
and o.order_id > 10;
SQL4:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
and o.user_id > 10;
These two statements seem to be after filtering the left table , Then associated with the right table , But he is not like this . actually , in any case , The left table tb_order All the data in will be displayed , Under the condition of limiting the left table , The content in the corresponding right table that meets the association conditions is forcibly set to empty . The effect of these two statements is similar to SQL2 It's the same , But the effect is hard to understand , It is generally not recommended to write .
SQL5:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
where u.user_id > 10;
SQL6:
select o.order_id,o.user_id,u.user_id,u.user_name
from tb_order o LEFT JOIN tb_user u
on o.user_id = u.user_id
where o.user_id > 10;
The result of these two statements is more interesting , Their effect is the same , Namely , Use left join on The result set of association is then according to where Conditions for screening .
Follow the above analysis , Seems to be , In this way SQL in , None of them is easy to understand , First, filter the contents of the left table , Then match with the right table . A better way is , Filter the left table with sub query , If you can't understand the above left join on where Medium where How to use , You can also filter the right table with sub queries .
SQL as follows :
select o.order_id,o.user_id,u.user_id,u.user_name
from ( select * from tb_order where order_id > 10 ) o LEFT JOIN tb_user u
on o.user_id = u.user_id;
select o.order_id,o.user_id,u.user_id,u.user_name
from ( select * from tb_order where order_id > 10 ) o LEFT JOIN (select * from tb_user where user_id > 20) u
on o.user_id = u.user_id;
边栏推荐
- Drools (5): drools basic syntax (3)
- Linear table -- stack and queue
- 在kettle使用循环来处理表中的数据
- QT连接sqlite数据库的错误及其修改办法
- Port forwarding summary
- VLAN trunk实验
- Py2exe QT interface style becomes Win98 solution
- MySQL quickly compares database table data
- MySQL query operation index optimization practice
- Jmeter: interface automation test - BeanShell compares database data and return data
猜你喜欢

Overall dichotomy?

Usage of string class

Federal Reserve SR 11-7: Guidance on model risk management - Wanzi collection

Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较

Drools (5): drools advanced syntax

C语言程序设计 | 程序编译与预处理

在mac中使用docker来搭建oracle数据库服务器

sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)

零号培训平台课程-2、SSRF基础

C# 常用功能整合-3
随机推荐
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
漏风的小棉袄……
Port forwarding summary
2022 0726 Gu Yujia's study notes
在mysql中同时使用left join on 和where 的查询结果分析
(2022 Niuke multi school III) a-ancestor (LCA)
美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏
A Competitive Swarm Optimizer for Large Scale Optimization
端口转发小结
Quartus: an error is reported when adding a.V file to someone else's project
"Weilai Cup" 2022 Niuke summer multi school training camp 1
docker安装MySQL8.0.28
12. Integer to Roman
Excuse me, is there a big delay in individual capture when someone uses Oracle xStream? How to solve the delay problem
MySQL2
Please ask the big guys a question. The pgsqlcdc task can't monitor changes after running for a period of time. Just restart it. What should I do
functools模块
MySQL: 提高最大连接数
Jmeter: interface automation test - BeanShell compares database data and return data
网络入门——vlan及trunk概述