当前位置:网站首页>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;
边栏推荐
猜你喜欢

一款开源 OA 办公自动化系统

Automatically generate UML sequence diagram according to text (draw.io format)

Bash: 创建返回布尔类型值的函数

Convert Excel to csv/csv UTF-8

美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏

SQLite 常用功能整合

A Competitive Swarm Optimizer for Large Scale Optimization

C4D动画如何提交云渲染农场快速渲染?

VLAN trunk实验

杂谈:最近好多朋友谈出国……
随机推荐
在kettle使用循环来处理表中的数据
请教大佬们一个问题,pgsqlcdc任务运行一段时间就不能监测变化了,重启就可以了,这个该从哪方面入
py2exe qt界面风格变成了win98解决方案
Linear table -- stack and queue
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
C# Winfrom 常用功能整合-2
Convert Excel to csv/csv UTF-8
C语言程序设计 | 程序编译与预处理
MySQL: 提高最大连接数
零号培训平台课程-2、SSRF基础
Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
用oracle来演示外键的使用
Go obtains the processing results of goroutine through channel
"Weilai Cup" 2022 Niuke summer multi school training camp 1
Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this
Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
【QT】capture.obj:-1: error: LNK2019: 无法解析的外部符号 __imp_htons(解决方法)
Algorithm -- Fibonacci sequence (kotlin)
Pg_relation_size 问题
VLAN trunk experiment