讓人又愛又恨的Mysql多表查詢

大雄45發表於2022-03-23
導讀 在SQL開發當中,多表聯查是絕對繞不開的一種技能。同樣的查詢結果不同的寫法其執行效率也是千差萬別。在實際開發當中,我見過(好像還寫過~)不少又長又臭的查詢SQL,資料量一上來查個十幾分鍾那是家常便飯。因此,深入理解SQL的多表查詢機制,少寫一些慢查詢,應該可以少挨點罵。
1. 前言

在SQL開發當中,多表聯查是絕對繞不開的一種技能。同樣的查詢結果不同的寫法其執行效率也是千差萬別。

在實際開發當中,我見過(好像還寫過~)不少又長又臭的查詢SQL,資料量一上來查個十幾分鍾那是家常便飯。

因此,深入理解SQL的多表查詢機制,少寫一些慢查詢,應該可以少挨點罵。

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

2. 等值連線和非等值連線
2.1 等值連線

等值連線是在多表查詢中最基礎,也最簡單的一種,其值為所有滿足條件的笛卡爾積。

在from後面,哪個表寫在前面結果中哪個表的值就先出現,如下:

select *
from student,
family
where student.family_id = family.id;

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

阿里在最新發布的Java開發手冊中強制要求,只要涉及多個表,必須在列名前加表的別名(或表名)進行限定

2.2 非等值連線

非等值連線是通過a表中的值在b表中的某一個範圍來進行的,能夠很好的滿足預設定好的分段統計需求。

非等值連線有兩種寫法,使用between...and...或大於號小於號

-- 第一種寫法:使用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;
-- 第二種寫法,使用>=或<=
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;

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

3. 自連線和非自連線
3.1 自連線

自連線,顧名思義就是同一張表自己跟自己連線,為了區分需要給表取不同的別名。如一張成績表,需要查詢所有分數比“語文”高的資料:

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

若不使用自連線,需要先通過查詢語文的分數,然後再查詢大於這個分數的資料。

具體可以按如下步驟進行查詢:

-- 先查詢語文的分數
select score from achievement where discipline_name = '語文';
-- 再查詢分數比語文分數更高的資料
select * from achievement where score > 76;

而使用自連線,則可以在一條sq語句裡完成查詢:

select a.*
from achievement a,
achievement b
where b.discipline_name = '語文'
and a.score > b.score;

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

3.2 非自連線

除自連線外,其他的都叫非自連線~~~

4. 內連線和外連線

內連線和外連線的區分本質上是另一種分類方法,如內連線就是等值連線。

  • 內連線:合併具有同一列的兩個或兩個以上的表的行, 結果集中不包含一個表與另一個表不匹配的行
  • 外連線:兩個表在連線過程中除了返回滿足連線條件的行以外還返回左(或右)表中不滿足條件的
  • 行 ,這種連線稱為左(或右) 外連線。沒有匹配的行時, 結果表中相應的列為空(NULL)。
  • 左外連線:連線條件中左邊的表也稱為主表 ,右邊的表稱為從表 。
  • 右外連線:連線條件中右邊的表也稱為主表 ,左邊的表稱為從表 。
  • 全外連線
4.1 測試資料

測試用學生表student和家庭表family資料如下:

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

4.2 左外連線
-- 查出student中的所有資料,不滿足的顯示為null
-- 這裡student在前面
select a.*
from student a
left join family b on a.family_id = b.id

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

4.3 右外連線
-- 查出student中的所有資料,不滿足的顯示為null
-- 這裡student在後面
select a.*
from family b
right join student a on b.id = a.family_id;

讓人又愛又恨的Mysql多表查詢讓人又愛又恨的Mysql多表查詢

4.4 全外連線

很遺憾,MySQL不支援全外連線。

附錄:測試資料SQL

-- auto-generated definition
create table student
(
    id           int auto_increment
        primary key,
    student_id   int                                null comment '學號',
    student_name varchar(40)                        null comment '姓名',
    family_id    int                                null comment '家庭ID',
    create_time  datetime default CURRENT_TIMESTAMP null comment '建立時間'
)
    comment '學生表';
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 '建立時間'
)
    comment '家庭表';
create table achievement
(
    id              int auto_increment
        primary key,
    score           int         null comment '分數',
    discipline_name varchar(40) null comment '學科名稱',
    student_id      int         null comment '學號'
)
    comment '成績表';
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 '建立時間'
)
    comment '分數檔次表';
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, '優秀', 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, '語文', 1);
INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (2, 80, '數學', 1);
INSERT INTO achievement (id, score, discipline_name, student_id) VALUES (3, 65, '英語', 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);

原文來自:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2883623/,如需轉載,請註明出處,否則將追究法律責任。

相關文章