1. 程式人生 > 資料庫 >MySQL資料庫之多表查詢using優化與案例

MySQL資料庫之多表查詢using優化與案例

using

  • 概念
    • using用來指定連線欄位
    • using的結果也會對公共欄位進行優化,優化的規則和自然連線是一樣的
MariaDB [sel]> select * from grades inner join resume using(name);
+-------+---------+------+----+-----------+
| name  | chinese | math | id | skill     |
+-------+---------+------+----+-----------+
| Sunny |      93 |   96 |  1 | php       |
| Jerry |      97 |   91 |  3 | php,mysql |
+-------+---------+------+----+-----------+
# `2 rows in set (0.001 sec)`

MySQL練習題

顯示地區及每個地區參加筆試的人數,並按人數降序排列

  • 思路分解
    • select 查詢欄位
    • from 多表查詢 左外連線
    • using 指定連線欄位
    • group by 分組查詢結果
    • order by 降序排列
-- 第一步: 顯示地區及每個地區參加筆試的人數
mysql> select stuaddress,count(writtenexam) from stuinfo left join stumarks using(stuno) group by stuaddress;
+------------+--------------------+
| stuaddress | count(writtenexam) |
+------------+--------------------+
| 上海       |                  1 |
| 北京       |                  2 |
| 天津       |                  2 |
| 河北       |                  0 |
| 河南       |                  0 |
+------------+--------------------+
# `5 rows in set (0.00 sec)`

-- 第二步:將結果降序排列
mysql> select stuaddress,count(writtenexam) c from stuinfo left join stumarks using(stuno) group by stuaddress order by c desc;
+------------+---+
| stuaddress | c |
+------------+---+
| 北京       | 2 |
| 天津       | 2 |
| 上海       | 1 |
| 河北       | 0 |
| 河南       | 0 |
+------------+---+
# `5 rows in set (0.00 sec)`

顯示有學生參加考試的地區

  • 思路解析
    • select 選擇查詢欄位
    • from 多表查詢 左外連線
    • using 指定連線欄位
    • group by 分組查詢顯示
    • having 條件篩選
-- having篩選
mysql> select stuaddress,count(writtenexam) c from stuinfo left join stumarks using(stuno) group by stuaddress having c>0;
+------------+---+
| stuaddress | c |
+------------+---+
| 上海       | 1 |
| 北京       | 2 |
| 天津       | 2 |
+------------+---+
# `3 rows in set (0.00 sec)`
  • 思路解析
    • select 選擇查詢欄位
    • from 多表查詢 右外連線
    • using 指定連線欄位
    • distinct 去重複
    • having 條件篩選
    • is not null 去空
-- 表連線實現
-- 第一步:右連接獲取有成績的地區
mysql> select stuaddress from stuinfo right join stumarks using(stuno);
+------------+
| stuaddress |
+------------+
| 北京       |
| 上海       |
| 天津       |
| 北京       |
| 天津       |
| NULL       |
+------------+
# `6 rows in set (0.00 sec)`

-- 第二步:去重複
mysql> select distinct stuaddress from stuinfo right join stumarks using(stuno);
+------------+
| stuaddress |
+------------+
| 北京       |
| 上海       |
| 天津       |
| NULL       |
+------------+
# `4 rows in set (0.00 sec)`

-- 去除null
mysql> select distinct stuaddress from stuinfo right join stumarks using(stuno) having stuaddress is not null;
+------------+
| stuaddress |
+------------+
| 北京       |
| 上海       |
| 天津       |
+------------+
# `3 rows in set (0.00 sec)`

顯示男生和女生的人數

  • 方法一:分組查詢
    • select 查詢欄位
    • from 查詢表
    • group by 分組查詢顯示
mysql> select stusex,count(*) from stuinfo group by stusex;
+--------+----------+
| stusex | count(*) |
+--------+----------+
| 女     |        3 |
| 男     |        4 |
+--------+----------+
# `2 rows in set (0.00 sec)`
  • 方法二:union
    • select 查詢欄位
    • from 查詢表
    • where 條件篩選
    • union 聯合查詢
mysql> select stusex,count(*) from stuinfo where stusex='男' union select stusex,count(*) from stuinfo where stusex='女';
+--------+----------+
| stusex | count(*) |
+--------+----------+
| 男     |        4 |
| 女     |        3 |
+--------+----------+
# `2 rows in set (0.00 sec)`
  • 方法三:直接寫條件
    • select 聚合函式查詢
    • from 查詢表
mysql> select sum(stusex='男') 男,sum(stusex='女') 女 from stuinfo;
+------+------+
| 男   | 女   |
+------+------+
|    4 |    3 |
+------+------+
# `1 row in set (0.00 sec)`

顯示每個地區男生、女生、總人數

  • 思路解析
    • select 選擇欄位 聚合函式
    • from 選擇表
    • group by 分組查詢顯示
mysql> select stuaddress,count(*) 總人數,sum(stusex='男') 男,sum(stusex='女') 女 from stuinfo group by stuaddress;
+------------+--------+------+------+
| stuaddress | 總人數 | 男    | 女   |
+------------+--------+------+------+
| 上海       |      1 |    1 |    0 |
| 北京       |      2 |    1 |    1 |
| 天津       |      2 |    2 |    0 |
| 河北       |      1 |    0 |    1 |
| 河南       |      1 |    0 |    1 |
+------------+--------+------+------+
# `5 rows in set (0.00 sec)`