当前位置:网站首页>[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
边栏推荐
- PR first time
- Redis 排查大 key 的4种方法,优化必备
- Es module and commonjs learning notes
- Ue4/ue5 illusory engine, material part (III), material optimization at different distances
- Establish cloth effect in 10 seconds
- Heap sort summary
- 669. Prune binary search tree ●●
- Page countdown
- Simple HelloWorld color change
- MySQL audit log archiving
猜你喜欢

UE fantasy engine, project structure

Collapse of adjacent vertical outer margins

2022/7/2做题总结

C语言杂谈1
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research

Bucket sort

Autocad-- dynamic zoom

2022/7/2 question summary

Chinese notes of unit particle system particle effect

嵌入式数据库开发编程(五)——DQL
随机推荐
GBase数据库助力湾区数字金融发展
China as resin Market Research and investment forecast report (2022 Edition)
被舆论盯上的蔚来,何时再次“起高楼”?
BUUCTF MISC
Stm32cubemx (8): RTC and RTC wake-up interrupt
Collapse of adjacent vertical outer margins
Development error notes
[转]MySQL操作实战(一):关键字 & 函数
UE 虚幻引擎,项目结构
Research on the value of background repeat of background tiling
嵌入式数据库开发编程(六)——C API
[LeetCode] 整数反转【7】
Autocad-- dynamic zoom
UE fantasy engine, project structure
[turn]: Apache Felix framework configuration properties
Lua determines whether the current time is the time of the day
[turn to] MySQL operation practice (I): Keywords & functions
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
Merge sort
Unity connects to the database