当前位置:网站首页>[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
边栏推荐
- 《动手学深度学习》学习笔记
- [转]:Apache Felix Framework配置属性
- Merge sort
- Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
- Create a pyGame window with a blue background
- Programmers' experience of delivering takeout
- MySQL audit log Archive
- [trans]: spécification osgi
- 2020-10-27
- Sqlserver stored procedures pass array parameters
猜你喜欢
随机推荐
Es module and commonjs learning notes
十年不用一次的JVM调用
stm32Cubemx(8):RTC和RTC唤醒中断
A three-dimensional button
Detailed explanation of the ranking of the best universities
669. Prune binary search tree ●●
Common database statements in unity
China as resin Market Research and investment forecast report (2022 Edition)
Merge sort
Embedded database development programming (V) -- DQL
[转]MySQL操作实战(三):表联结
Sixth note
xftp7与xshell7下载(官网)
嵌入式数据库开发编程(六)——C API
Unity shot tracking object
[轉]: OSGI規範 深入淺出
AutoCAD - full screen display
[turn]: OSGi specification in simple terms
cocos_ Lua listview loads too much data
Unity3d learning notes









