当前位置:网站首页>[turn to] MySQL operation practice (III): table connection
[turn to] MySQL operation practice (III): table connection
2022-07-05 05:14:00 【morpheusWB】
MySQL Practical operation ( 3、 ... and ): Table join

1. Simple connection
① inner Join
Internal connection is also called equivalent connection .ANSI SQL Specifications are preferred INNER JOIN grammar .
Inner join is divided into implicit inner join and explicit inner join , The two queries have the same effect , It's just that the grammar is different .
Implicit inner connection :WHERE Clause
FROM tb1_name, tb2_name, tb3_name
WHERE condition_1 AND condition_2
Explicit inner join :INNER JOIN grammar
FROM tb1_name
INNER JOIN tb2_name ON condition_1
INNER JOIN tb3_name ON condition_2
Application scenarios : Count the students who took the exam and their grades in various subjects
# Implicit inner connection
mysql> SELECT a.stu_id, stu_name, a.lesson_id, lesson_name, score -> FROM t_score a, t_stu_profile b, t_lesson c -> WHERE a.stu_id = b.stu_id AND a.lesson_id = c.lesson_id -> ORDER BY stu_id, lesson_id -> ;+--------+----------+-----------+-------------+-------+| stu_id | stu_name | lesson_id | lesson_name | score |+--------+----------+-----------+-------------+-------+| 1 | Guo Dong | L001 | Chinese language and literature | 90 || 1 | Guo Dong | L002 | data | 86 || 2 | Lixi | L001 | Chinese language and literature | 84 || 2 | Lixi | L002 | data | 90 || 2 | Lixi | L003 | English | 86 || 2 | Lixi | L004 | Physics | 75 || 2 | Lixi | L005 | chemical | 77 || 3 | Zhang Bei | L001 | Chinese language and literature | 100 || 3 | Zhang Bei | L002 | data | 91 || 3 | Zhang Bei | L003 | English | 85 || 4 | Qian Nan | L001 | Chinese language and literature | 99 || 4 | Qian Nan | L002 | data | 88 || 4 | Qian Nan | L003 | English | 66 || 4 | Qian Nan | L005 | chemical | 98 |+--------+----------+-----------+-------------+-------+# Explicit inner join
mysql> SELECT a.stu_id, stu_name, a.lesson_id, lesson_name, score -> FROM t_score a -> INNER JOIN t_stu_profile b -> ON a.stu_id = b.stu_id -> INNER JOIN t_lesson c -> ON a.lesson_id = c.lesson_id -> ORDER BY stu_id, lesson_id -> ;
2. Advanced connection
① Self coupling
Self join is usually used as an external statement instead of a subquery statement used to retrieve data from the same table . Although the final result is the same , But sometimes processing joins is much faster than processing subqueries .
Application scenarios : I found a 100 There is a problem with the score , Need to inquire 100 Is there any problem with the grades of other students in this course
Subquery : First find 100 The name of this course (lesson_id), Then find out the grades of other students in this course .
mysql> SELECT stu_id, score -> FROM t_score -> WHERE lesson_id = ( -> SELECT lesson_id -> FROM t_score -> WHERE score = 100) -> ;+--------+-------+| stu_id | score |+--------+-------+| 1 | 98 || 2 | 84 || 3 | 100 || 4 | 99 |+--------+-------+
Self coupling : The two tables needed in this query are actually the same table , therefore t_score Table in FROM The clause appears twice . Although it's legal , But yes t_score Table references are ambiguous , because MySQL I don't know the quotation is t_score Which instance of the table .
mysql> SELECT a.stu_id, a.score -> FROM t_score a, t_score b -> WHERE a.lesson_id = b.lesson_id AND b.score = 100 -> ;+--------+-------+| stu_id | score |+--------+-------+| 1 | 98 || 2 | 84 || 3 | 100 || 4 | 99 |+--------+-------+② natural join
Whenever tables are joined , There should be at least one column in more than one table ( Joined columns ). The standard join returns all data , Even the same column appears multiple times .
Natural connections exclude multiple occurrences , Make each column return only once .
wildcard * Only for watches t_stu_profile Use , All other columns are clearly listed , So no duplicate columns are retrieved .
mysql> SELECT b.*, a.lesson_id, c.lesson_name, a.score -> FROM t_score a, t_stu_profile b, t_lesson c -> WHERE a.stu_id = b.stu_id AND a.lesson_id = c.lesson_id -> ;
in fact , Every internal connection established so far is a natural connection , It is likely that internal connections that are not natural connections will never be used .
③ outer join
Many joins associate rows in one table with rows in another , But sometimes you need to include rows that have no associated rows . for example , You need to count how many orders each customer has placed , Including customers who have not yet placed orders .
that , A join contains rows that have no associated rows in the related table , This type of connection is called an external connection .
Application scenarios : Count the grades of each student , Including students who did not take the exam
outer join : The external connection is divided into left external connection (LEFT OUTER JOIN) And right outer connection (RIGHT OUTER JOIN)
mysql> SELECT a.stu_id, stu_name, lesson_id, score -> FROM t_score a -> RIGHT OUTER JOIN t_stu_profile b -> ON a.stu_id = b.stu_id -> ;+--------+----------+-----------+-------+| stu_id | stu_name | lesson_id | score |+--------+----------+-----------+-------+| 1 | Guo Dong | L001 | 98 || 1 | Guo Dong | L002 | 86 || 1 | Guo Dong | L003 | 79 || 1 | Guo Dong | L004 | 88 || 1 | Guo Dong | L005 | 98 || 2 | Lixi | L001 | 84 || 2 | Lixi | L002 | 90 || 2 | Lixi | L003 | 86 || 2 | Lixi | L004 | 75 || 2 | Lixi | L005 | 77 || 3 | Zhang Bei | L001 | 100 || 3 | Zhang Bei | L002 | 91 || 3 | Zhang Bei | L003 | 85 || 3 | Zhang Bei | L004 | 79 || 3 | Zhang Bei | L005 | 85 || 4 | Qian Nan | L001 | 99 || 4 | Qian Nan | L002 | 88 || 4 | Qian Nan | L003 | 66 || 4 | Qian Nan | L004 | 66 || 4 | Qian Nan | L005 | 98 || NULL | Wang Wu | NULL | NULL || NULL | Zhao Qi | NULL | NULL |+--------+----------+-----------+-------+
————————————————
Copyright notice : This paper is about CSDN Blogger 「lulin916」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_39010770/article/details/87886673
边栏推荐
- Sixth note
- LeetCode之單詞搜索(回溯法求解)
- Cocos create Jiugongge pictures
- [LeetCode] 整数反转【7】
- Es module and commonjs learning notes -- ESM and CJS used in nodejs
- Chinese notes of unit particle system particle effect
- Solon 框架如何方便获取每个请求的响应时间?
- [转]MySQL操作实战(三):表联结
- Research on the value of background repeat of background tiling
- cocos_ Lua listview loads too much data
猜你喜欢

Heap sort summary

Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!

Collapse of adjacent vertical outer margins

stm32Cubemx(8):RTC和RTC唤醒中断
![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[轉]: OSGI規範 深入淺出

Shell Sort

UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化

Recherche de mots pour leetcode (solution rétrospective)
![[turn]: OSGi specification in simple terms](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[turn]: OSGi specification in simple terms

54. Spiral matrix & 59 Spiral matrix II ●●
随机推荐
MD5 bypass
Panel panel of UI
Insert sort
A three-dimensional button
Unity3d learning notes
"Measuring curve length" of CAD dream drawing
Embedded database development programming (VI) -- C API
Download and use of font icons
Autocad-- dynamic zoom
mysql審計日志歸檔
Simple modal box
2020-10-27
Listview is added and deleted at the index
AutoCAD - set layer
Time format conversion
小程序直播+電商,想做新零售電商就用它吧!
Page countdown
Do a small pressure test with JMeter tool
2022上半年全国教师资格证下
Es module and commonjs learning notes