当前位置:网站首页>[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
边栏推荐
- Unity card flipping effect
- LeetCode之單詞搜索(回溯法求解)
- django连接数据库报错,这是什么原因
- 嵌入式数据库开发编程(五)——DQL
- 【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
- Cocos create Jiugongge pictures
- When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- Lua determines whether the current time is the time of the day
- 2022年上半年国家教师资格证考试
猜你喜欢
Embedded database development programming (zero)
Unity find the coordinates of a point on the circle
Bucket sort
Generate filled text and pictures
Learning notes of "hands on learning in depth"
Autocad-- dynamic zoom
AutoCAD - set layer
Page countdown
Leetcode word search (backtracking method)
C4D simple cloth (version above R21)
随机推荐
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Unity enables mobile phone vibration
669. Prune binary search tree ●●
Create a pyGame window with a blue background
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Unity synergy
2022/7/2 question summary
嵌入式数据库开发编程(五)——DQL
小程序直播+電商,想做新零售電商就用它吧!
UE fantasy engine, project structure
Embedded database development programming (V) -- DQL
Es module and commonjs learning notes -- ESM and CJS used in nodejs
2022/7/2做题总结
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Vs2015 secret key
Page countdown
Listview pull-down loading function
Basic knowledge points of dictionary
Basic knowledge points