当前位置:网站首页>Where, on when MySQL external connection is used
Where, on when MySQL external connection is used
2022-04-23 05:08:00 【Write poetry for you@】
elicit questions
Example :
1. Query the product information without sales records , And product information with sales records
test , Using internal connection is as simple as connecting two tables
SELECT * FROM goods JOIN Sales list ON Sales list . Product id = goods . Product id
External connection two tables are connected
SELECT * FROM goods LEFT JOIN Sales list ON Sales list . Product id = goods . Product id
ps: There is an additional line of product information without sales records .
The results have come out , That is, because in the commodity list g16 This item number does not match in the sales table , The sales form doesn't have g16 Sales record of , But because of the external connection , Itself is to let : Table on the left ( goods ) Even if some fields ( Product id ) The record is not associated with another table ( Sales list ) Field matching will also show , ps: Therefore, the commodity number in the commodity table is G16 This record will show . You can also understand why the sales table on the right side is empty ,【 No information matching the left table 】
Here's a simple test : In commodities The item number is 16 The record of is consistent with the above figure .
Throw questions , The main below
Now I want to query the product information without sales record . Is to take null Filter out the record of .
Positive solution :
SELECT * FROM goods LEFT JOIN Sales list ON Sales list . Product id = goods . Product id
WHERE Sales list . Number IS NULL
ps: I used to write my own way :
Why can't tables be connected on Followed by filter conditions ???
SELECT * FROM goods LEFT JOIN Sales list ON Sales list . Product id = goods . Product id
AND Sales list . Number IS NULL
As a result, the records whose sales table is not empty are also displayed , All in null fill . This does not meet the requirements
This may not be easy to understand , Direct tests left join Back on + Filter conditions and where + Filter conditions
where Filter
sql: Execution order ; 1.join left on When connected according to conditions , There are no other filter conditions , Generate temporary tables
2. After the temporary table is generated ( It's a big watch connected by two watches ), Add filtering conditions where , here Not in line with where Conditions Of remove
3.select Inquire about
on When connected +and Filter
sql Execution order : 1.join left on When connected according to conditions , That is, when the temporary table is being generated and filter , No matter and The condition is true and false , The records on the left show , 2.select Inquire about
As a result, the sales provinces and cities are not all in Shanxi null fill , It's not filtered into a record we want
summary : In fact, that is left join Connected to a where And on The timing of implementation is different
In the use of left join when ,on and where The conditions differ as follows : 1、 on The condition is the condition used to generate the temporary table , It doesn't care on Is the condition in true , Will return to the records in the table on the left , The right table does not meet the conditions null fill 2、where The condition is that after the temporary table is generated , Then filter the temporary table . There is no such thing as left join The meaning of the , The temporary table has been generated , If the conditions are not true, filter them out
版权声明
本文为[Write poetry for you@]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220549552190.html
边栏推荐
- Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
- 【数据库】MySQL基本操作(基操~)
- Unity C# 网络学习(四)
- 直播带货表格模板-自动显示图片-自动关联系列商品
- 独立站运营 | FaceBook营销神器——聊天机器人ManyChat
- This call when the transaction does not take effect
- [2021] Spatio-Temporal Graph Contrastive Learning
- How to exit VIM
- Innovation training (II) task division
- Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
猜你喜欢
跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
[database] MySQL multi table query (I)
[WinUI3]編寫一個仿Explorer文件管理器
[database] MySQL single table query
Transaction isolation level of MySQL transactions
Independent station operation | Facebook marketing artifact - chat robot manychat
redis数据类型有哪些
At pgconf Asia Chinese technology forum, listen to Tencent cloud experts' in-depth understanding of database technology
Restful toolkit of idea plug-in
【数据库】MySQL基本操作(基操~)
随机推荐
Learning Android II from scratch - activity
Docker installation and mysql5 7 installation
MySQL memo (for your own query)
Logrus set log format and output function name
mysql5. 7. X data authorization leads to 1141
Luogu p2731 horse riding fence repair
Details related to fingerprint payment
[winui3] write an imitation Explorer file manager
A trinomial expression that causes a null pointer
Live delivery form template - automatically display pictures - automatically associate series products
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
Unique primary key ID of tidb sub table -- solution to failure of sequence and Gorm to obtain primary key
One month countdown, pgconf What are the highlights of the latest outlook of asia2021 Asian Conference?
[WinUI3]编写一个仿Explorer文件管理器
configmap
Agile practice | agile indicators to improve group predictability
Grpc long connection keepalive
负载均衡简介
和谐宿舍(线性dp / 区间dp)
C. Tree Infection(模拟+贪心)