当前位置:网站首页>Left connection and right connection of MySQL (the difference between inner connection and natural connection)
Left connection and right connection of MySQL (the difference between inner connection and natural connection)
2022-07-28 11:25:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
obtain boy 、girl Table download address
One 、 Internal connection query inner join
keyword :inner join on
sentence :select * from a_table a inner join b_table b on a.a_id = b.b_id;
explain : Combine records from two tables , Return the records matching the associated fields , That is, return the intersection of two tables ( shadow ) part .
Case explanation : stay boy Table and girl There are two tables in the table hid Field consistent name (gname,bname),boy Table and girl The table is as follows :
Internal connection query method is adopted :
SELECT boy.hid,boy.bname,girl.gname FROM boy INNER JOIN girl ON girl.hid = boy.hid;The query results are as follows :
Two 、 Left connection query left join
keyword :left join on / left outer join on
sentence :SELECT * FROM a_table a left join b_table b ON a.a_id = b.b_id;
explain : left join yes left outer join Abbreviation , Its full name is left outer join , It's one of the outer connections . Left ( Outside ) Connect , The left table (a_table) All of the records will be represented , The right table (b_table) Only records that match the search criteria are displayed . The missing records in the right table are NULL.
Case explanation : stay boy Table and girl Left connection query in the table ,boy Table and girl The table is as follows :
Internal connection query method is adopted :
SELECT boy.hid,boy.bname,girl.gname FROM boy LEFT JOIN girl ON girl.hid = boy.hid;The query results are as follows :
3、 ... and 、 The right connection right join
keyword :right join on / right outer join on
sentence :SELECT * FROM a_table a right outer join b_table b on a.a_id = b.b_id;
explain :right join yes right outer join Abbreviation , Its full name is right outer connection , It's one of the outer connections . And left ( Outside ) The connection is the opposite , Right ( Outside ) Connect , The left table (a_table) Only records that match the search criteria are displayed , The right table (b_table) All of the records will be represented . All the places that are not recorded in the left table are NULL.
Case explanation : stay boy Table and girl Right link query in the table ,boy Table and girl The table is as follows :
Internal connection query method is adopted :
SELECT boy.hid,boy.bname,girl.gname FROM boy RIGHT JOIN girl ON girl.hid = boy.hid;The query results are as follows :
Four 、 Full connection union
keyword :union /union all
sentence :(select colum1,colum2…columN from tableA ) union (select colum1,colum2…columN from tableB )
or (select colum1,colum2…columN from tableA ) union all (select colum1,colum2…columN from tableB );
union Statement notes :
1. adopt union Connected SQL They must have the same number of columns taken out separately ;
2. Do not require the merged table column names to be the same , In the first sql The names listed in the table shall prevail ;
3. Use union when , Exactly equal lines , Will be merged , Because merging is time consuming , Generally, it is not used directly union A merger , It's usually used union all A merger ;
4. By union Connected sql Clause , Don't write in a single sentence order by , Because there will be no sorting effect . But you can sort the final result set ;
(select id,name from A order by id) union all (select id,name from B order by id); // No sorting effect
(select id,name from A ) union all (select id,name from B ) order by id; // It has sorting effect
Case explanation : take a Table and b Table merge , The table structure is as follows :
use union Full connection :
union It will automatically remove the completely repeated data ,a、b In the table ”c” The values are 15, So just one line .
use union all Full connection :
union all Will keep those duplicate data ;
Left and right connecting exercises :
Write... As required according to the given table structure SQL sentence .t surface ( namely Team surface ) and m surface ( namely Match surface ) The structure is as follows :
t surface ( namely Team surface ) and m surface ( namely Match surface ) Is as follows :
t Table and m Table download address
m surface ( namely Match surface ) Of hostTeamID And guestTeamID Both with t surface ( namely Team surface ) Medium teamID relation . Please find out 2006-6-1 To 2006-7-1 All the competitions between , And list... In the following form : Bayern Munich 2:0 Bremen 2006-6-21
===============================================================================
Solution :
First step : Start with m Table left connection t surface , Find out m In the table hid This column corresponds to the competition information :
SELECT m.mid,t.tname,m.mres,m.matime as time FROM m LEFT JOIN t ON t.tid = m.hid;Query results are recorded as result sets t1 ,t1 The table is as follows :
The second step : Start with m Table left connection t surface , Find out m In the table gid This column corresponds to the competition information :
SELECT m.mid,t.tname,m.mres,m.matime FROM m LEFT JOIN t ON t.tid = m.gid;Query results are recorded as result sets t2 ,t2 The table is as follows :
The third step : As a result set t1 Left join query result set based on t2, The query condition is the match number of both (mid) identical .
SELECT t1.tname,t1.mres,t2.tname,t1.time FROM
(SELECT m.mid,t.tname,m.mres,m.matime as time FROM m LEFT JOIN t ON t.tid = m.hid)
as t1
LEFT JOIN
(SELECT m.mid,t.tname,m.mres,m.matime as time FROM m LEFT JOIN t ON t.tid = m.gid)
as t2
ON t1.mid = t2.mid WHERE t1.time BETWEEN '2006-06-01' AND '2006-07-01';The query results are as follows :
Full connection exercises :
A Table and B The table structure is as follows , Please merge the two tables :
Merger requirements :A In the table a:5,B In the table a:5, Therefore, in the consolidated table a The corresponding value is 10; The samples of results required are as follows :
use union all Full connection , And then use from Subquery :
SELECT id,SUM(num) as num FROM ((SELECT id,num FROM a) UNION ALL(SELECT id,num FROM b)) as tb GROUP BY id;Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/128270.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

ripro9.0修正升级版+WP两款美化包+稀有插件

What functions does MySQL have? Don't look everywhere. Just look at this.

重新刷新你对Redis集群的理解

Inventory: 6 books teach you the necessary skills for career promotion

Inventory: 144 free learning websites, the most complete collection of resources in the whole network

Learning notes tree array

PHP发送移动MAS短信乱码的解决方法

小水滴2.0网站导航网模板

Zero code | easily realize data warehouse modeling and build Bi Kanban

BC35 NB模块AT指令开发总结
随机推荐
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
融云 IM & RTC 能力上新盘点
Two point, three point, 01 point plan [bullet I]
Nodejs: set up the express service, set up the session and realize the exit operation
完整版H5社交聊天平台源码[完整数据库+完整文档教程]
Understand several concepts of Oracle
用手机对电脑进行远程关机
Flutter教程之带有 GoRouter 的 Flutter Navigator 2.0,使用 go_router 包了解 Flutter 中的声明式路由机制(教程含源码)
JS - 修改数组中对象的键名
echo -ne(echo line)
「学习笔记」树状数组
苹果手机iCloud钥匙串的加密缺陷
机器学习强基计划0-5:为什么学习的本质是泛化能力?
ripro9.0修正升级版+WP两款美化包+稀有插件
Question of hanging the interviewer
win10安装sqlmap(windows 7)
Display line number under VIM command [easy to understand]
接口测试的作用
Relevant knowledge points of hash table
C语言使用二重指针实现简单工厂模式(多态)