MySQL多表查詢機(jī)制
1. 前言
在SQL開(kāi)發(fā)當(dāng)中,多表聯(lián)查是絕對(duì)繞不開(kāi)的一種技能。同樣的查詢結(jié)果不同的寫(xiě)法其運(yùn)行效率也是千差萬(wàn)別。
在實(shí)際開(kāi)發(fā)當(dāng)中,我見(jiàn)過(guò)(好像還寫(xiě)過(guò)~)不少又長(zhǎng)又臭的查詢SQL,數(shù)據(jù)量一上來(lái)查個(gè)十幾分鐘那是家常便飯。
因此,深入理解SQL的多表查詢機(jī)制,少寫(xiě)一些慢查詢,應(yīng)該可以少挨點(diǎn)罵。

2. 等值連接和非等值連接
2.1 等值連接
等值連接是在多表查詢中最基礎(chǔ),也最簡(jiǎn)單的一種,其值為所有滿足條件的笛卡爾積。
在from后面,哪個(gè)表寫(xiě)在前面結(jié)果中哪個(gè)表的值就先出現(xiàn),
如下:
select * from student, ? ? ?family where student.family_id = family.id;
等值連接查詢結(jié)果:

阿里在最新發(fā)布的Java開(kāi)發(fā)手冊(cè)中強(qiáng)制要求,只要涉及多個(gè)表,必須在列名前加表的別名(或表名)進(jìn)行限定

2.2 非等值連接
非等值連接是通過(guò)a表中的值在b表中的某一個(gè)范圍來(lái)進(jìn)行的,能夠很好的滿足預(yù)設(shè)定好的分段統(tǒng)計(jì)需求。
非等值連接有兩種寫(xiě)法,使用between...and...或大于號(hào)小于號(hào)
第一種寫(xiě)法:使用between...and...
select a.discipline_name, a.score, b.grade_tag from achievement a, ? ? ?achievement_grade b where a.score between b.lowest_score and b.highest_score;
第二種寫(xiě)法:使用>=或<=
select a.discipline_name, a.score, b.grade_tag from achievement a, ? ? ?achievement_grade b where a.score >= b.lowest_score ? and a.score <= b.highest_score;
非等值連接查詢結(jié)果:

3. 自連接和非自連接
3.1 自連接
自連接,顧名思義就是同一張表自己跟自己連接,為了區(qū)分需要給表取不同的別名。如一張成績(jī)表,需要查詢所有分?jǐn)?shù)比“語(yǔ)文”高的數(shù)據(jù):
分?jǐn)?shù)表:

若不使用自連接,需要先通過(guò)查詢語(yǔ)文的分?jǐn)?shù),然后再查詢大于這個(gè)分?jǐn)?shù)的數(shù)據(jù)。
具體可以按如下步驟進(jìn)行查詢:
-- 先查詢語(yǔ)文的分?jǐn)?shù) select score from achievement where discipline_name = '語(yǔ)文'; -- 再查詢分?jǐn)?shù)比語(yǔ)文分?jǐn)?shù)更高的數(shù)據(jù) select * from achievement where score > 76;
而使用自連接,則可以在一條sq語(yǔ)句里完成查詢:
select a.* from achievement a, ? ? ?achievement b where b.discipline_name = '語(yǔ)文' ? and a.score > b.score;
自連接查詢結(jié)果:

3.2 非自連接
除自連接外,其他的都叫非自連接~~~
4. 內(nèi)連接和外連接
內(nèi)連接和外連接的區(qū)分本質(zhì)上是另一種分類方法,如內(nèi)連接就是等值連接。
- 內(nèi)連接:合并具有同一列的兩個(gè)或兩個(gè)以上的表的行, 結(jié)果集中不包含一個(gè)表與另一個(gè)表不匹配的行
- 外連接:兩個(gè)表在連接過(guò)程中除了返回滿足連接條件的行以外還返回左(或右)表中不滿足條件的
行 ,這種連接稱為左(或右) 外連接。沒(méi)有匹配的行時(shí), 結(jié)果表中相應(yīng)的列為空(NULL)。
- 左外連接:連接條件中左邊的表也稱為主表 ,右邊的表稱為從表 。
- 右外連接:連接條件中右邊的表也稱為主表 ,左邊的表稱為從表 。
- 全外連接
4.1 測(cè)試數(shù)據(jù)
測(cè)試用學(xué)生表student和家庭表family數(shù)據(jù)如下:
學(xué)生表數(shù)據(jù):

家庭表數(shù)據(jù):

4.2 左外連接
-- 查出student中的所有數(shù)據(jù),不滿足的顯示為null -- 這里student在前面 select a.* from student a ? ? ? ? ?left join family b on a.family_id = b.id
左外連接查詢結(jié)果:

4.3 右外連接
-- 查出student中的所有數(shù)據(jù),不滿足的顯示為null -- 這里student在后面 select a.* from family b ? ? ? ? ?right join student a on b.id = a.family_id;
右外連接查詢結(jié)果:

4.4 全外連接
很遺憾,MySQL不支持全外連接。
附錄:測(cè)試數(shù)據(jù)SQL腳本
-- auto-generated definition create table student ( ? ? id ? ? ? ? ? int auto_increment ? ? ? ? primary key, ? ? student_id ? int ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?null comment '學(xué)號(hào)', ? ? student_name varchar(40) ? ? ? ? ? ? ? ? ? ? ? ?null comment '姓名', ? ? family_id ? ?int ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?null comment '家庭ID', ? ? create_time ?datetime default CURRENT_TIMESTAMP null comment '創(chuàng)建時(shí)間' ) ? ? comment '學(xué)生表'; create table family ( ? ? id ? ? ? ? ? ? int auto_increment ? ? ? ? primary key, ? ? family_name ? ?varchar(40) ? ? ? ? ? ? ? ? ? ? ? ?null comment '家庭名稱', ? ? family_address varchar(40) ? ? ? ? ? ? ? ? ? ? ? ?null comment '家庭地址', ? ? create_time ? ?datetime default CURRENT_TIMESTAMP null comment '創(chuàng)建時(shí)間' ) ? ? comment '家庭表'; create table achievement ( ? ? id ? ? ? ? ? ? ?int auto_increment ? ? ? ? primary key, ? ? score ? ? ? ? ? int ? ? ? ? null comment '分?jǐn)?shù)', ? ? discipline_name varchar(40) null comment '學(xué)科名稱', ? ? student_id ? ? ?int ? ? ? ? null comment '學(xué)號(hào)' ) ? ? comment '成績(jī)表'; create table achievement_grade ( ? ? id ? ? ? ? ? ?int auto_increment ? ? ? ? primary key, ? ? grade_tag ? ? varchar(10) ? ? ? ? ? ? ? ? ? ? ? ?null comment '檔次', ? ? lowest_score ?int ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?null comment '最低分', ? ? highest_score int ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?null comment '最高分', ? ? create_time ? datetime default CURRENT_TIMESTAMP null comment '創(chuàng)建時(shí)間' ) ? ? comment '分?jǐn)?shù)檔次表'; INSERT INTO achievement_grade (id, grade_tag, lowest_score, highest_score, create_time) VALUES (1, '不及格', 0, 60, '2022-03-02 11:44:01'); INSERT INTO achievement_grade (id, grade_tag, lowest_score, highest_score, create_time) VALUES (2, '良好', 60, 80, '2022-03-02 11:44:01'); INSERT INTO achievement_grade (id, grade_tag, lowest_score, highest_score, create_time) VALUES (3, '優(yōu)秀', 80, 100, '2022-03-02 11:44:01'); INSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (1, 1, '張三', 1, '2022-03-02 09:55:01'); INSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (2, 2, '李四', 2, '2022-03-02 09:55:01'); INSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (3, 3, '王五', 3, '2022-03-02 09:55:01'); INSERT INTO student (id, student_id, student_name, family_id, create_time) VALUES (4, 4, '高飛', null, '2022-03-02 19:45:14'); INSERT INTO family (id, family_name, family_address, create_time) VALUES (1, '張三家', '北京', '2022-03-02 09:54:13'); INSERT INTO family (id, family_name, family_address, create_time) VALUES (2, '李四家', '上海', '2022-03-02 09:54:13'); INSERT INTO family (id, family_name, family_address, create_time) VALUES (3, '王五家', '西伯利亞', '2022-03-02 09:54:13'); INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (1, 76, '語(yǔ)文', 1); INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (2, 80, '數(shù)學(xué)', 1); INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (3, 65, '英語(yǔ)', 1); INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (4, 98, '地理', 1); INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (5, 77, '歷史', 1); INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (6, 69, '生物', 1);
到此這篇關(guān)于MySQL多表查詢機(jī)制的文章就介紹到這了,更多相關(guān)MySQL多表查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL 中 datetime 和 timestamp 的區(qū)別與選擇
MySQL 中常用的兩種時(shí)間儲(chǔ)存類型分別是datetime和 timestamp。如何在它們之間選擇是建表時(shí)必要的考慮。下面就談?wù)勊麄兊膮^(qū)別和怎么選擇,需要的朋友可以參考一下2021-09-09
完美解決mysql啟動(dòng)后隨即關(guān)閉的問(wèn)題(ibdata1文件損壞導(dǎo)致)
下面小編就為大家?guī)?lái)一篇完美解決mysql啟動(dòng)后隨即關(guān)閉的問(wèn)題(ibdata1文件損壞導(dǎo)致)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Mysql 存在多條數(shù)據(jù)時(shí)如何按時(shí)間取最新的那一組數(shù)據(jù)(思路詳解)
這篇文章主要介紹了Mysql 存在多條數(shù)據(jù)時(shí)如何按時(shí)間取最新的那一組數(shù)據(jù),本文給大家分享兩種思路結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
Mysql大表全表update的的實(shí)現(xiàn)
有些時(shí)候在進(jìn)行一些業(yè)務(wù)迭代時(shí)需要我們對(duì)Mysql表中數(shù)據(jù)進(jìn)行全表update,本文主要介紹了Mysql大表update的的實(shí)現(xiàn)2024-08-08
mysql中int(3)和int(10)的數(shù)值范圍是否相同
依稀還記得有次面試,有面試官問(wèn)我int(10)與int(11)有什么區(qū)別,當(dāng)時(shí)覺(jué)得就是長(zhǎng)度的區(qū)別吧,后來(lái)發(fā)現(xiàn)事情不是這么簡(jiǎn)單,這篇文章主要給大家介紹了關(guān)于mysql中int(3)和int(10)的數(shù)值范圍是否相同的相關(guān)資料2021-10-10

