Join 結合查詢

  1. 到目前為止,所有的詢問都只針對一個資料表,但實際工作時,往往是結合多個工作表的詢問。
  2. 為了示範 JOIN 查詢,必須在資料庫 dyw 新增一個與資料表 tbl 相關的資料表 dcount,包含 author 及 dcount 兩個欄位,其中 author 與資料表 tbl 中的欄位 author 是相關的資料,
  3. 登入 mariadb。
    [root@kvm3 html]# mycli -uroot -p123qwe
    Connecting to socket /var/lib/mysql/mysql.sock, owned by user mysql
    MariaDB 10.5.16
    mycli 1.26.1
    Home: http://mycli.net
    Bug tracker: https://github.com/dbcli/mycli/issues
    Thanks to the contributor - Lewis Peckover
    
  4. 使用資料庫 dyw。
    MariaDB root@(none):(none)> use dyw;
    You are now connected to database "dyw" as user "root"
    Time: 0.003s
    Time: 0.147s
    
  5. 新增資料表 dcount,包含 author 及 dcount 兩個欄位。
    MariaDB root@(none):dyw> create table dcount(author varchar(40) not null, dcount
                          -> int);
    Query OK, 0 rows affected
    
  6. 在資料表 dcount 插入 4 筆紀錄。
    MariaDB root@(none):dyw> insert into dcount(author,dcount) values('dywang',4),('l
                          -> inda',3),('peter',2),('rita',1);
    Query OK, 4 rows affected
    Time: 0.105s
    
  7. 查詢資料表 dcount 共有 4 筆紀錄。
    MariaDB root@(none):dyw> select * from dcount;
    +--------+--------+
    | author | dcount |
    +--------+--------+
    | dywang | 4      |
    | linda  | 3      |
    | peter  | 2      |
    | rita   | 1      |
    +--------+--------+
    
    4 rows in set
    Time: 0.045s
    
  8. 查詢資料表 tbl 共有 3 筆紀錄。
    MariaDB root@(none):dyw> select * from tbl;
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | c++     | peter  | 2023-03-30      |
    | 2  | Linux   | linux  | 2023-01-30      |
    | 4  | ansible | dywang | 2023-04-30      |
    +----+---------+--------+-----------------+
    
    3 rows in set
    Time: 0.030s
    
  9. JOIN 結合兩個資料表 tbl 及 dcount 的查詢,FROM 後的兩個資料表各給 a 及 b 來代表,WHERE 則限制兩個資料表同時出現的紀錄。 查詢方式是搜尋欄位 author 同時出現在資料表 a 與 b 的紀錄。JOIN 也可使用逗號「,」代替。
    MariaDB root@(none):dyw> select a.id,a.author,b.dcount from tbl a JOIN dcount b w
                          -> here a.author=b.author;
    +----+--------+--------+
    | id | author | dcount |
    +----+--------+--------+
    | 4  | dywang | 4      |
    | 1  | peter  | 2      |
    +----+--------+--------+
    
    2 rows in set
    Time: 0.043s
    
  10. LEFT JOIN 不同於 JOIN,是選擇左邊資料表所有紀錄,若右邊資料表有共同的紀錄則一起列出。 因為資料表 dcount 中並沒有 author='linux',所以這筆紀錄的 dcount 為 NULL。
    MariaDB root@(none):dyw> select a.id,a.author,b.dcount from tbl a LEFT JOIN dcoun
                          -> t b on a.author=b.author;
    +----+--------+--------+
    | id | author | dcount |
    +----+--------+--------+
    | 4  | dywang | 4      |
    | 1  | peter  | 2      |
    | 2  | linux  | <null> |
    +----+--------+--------+
    
    3 rows in set
    Time: 0.024s
    
  11. RIGHT JOIN 則是選擇右邊資料表所有紀錄,若邊資料表有共同的紀錄則一起列出。 因為資料表 tbl 中並沒有 author='linda' 及 author='rita',所以這兩筆紀錄的 id 為 NULL。
    MariaDB root@(none):dyw> select a.id,a.author,b.dcount from tbl a RIGHT JOIN dcou
                          -> nt b on a.author=b.author;
    +--------+--------+--------+
    | id     | author | dcount |
    +--------+--------+--------+
    | 1      | peter  | 2      |
    | 4      | dywang | 4      |
    | <null> | <null> | 3      |
    | <null> | <null> | 1      |
    +--------+--------+--------+
    
    4 rows in set
    Time: 0.025s