1. 程式人生 > >資料庫_MySQL多表_查詢_子查詢

資料庫_MySQL多表_查詢_子查詢

01.MySQL多表_建表原則:
1).要保證每個列只記錄一個數據,如果一個列記錄了多條資料,那麼應該拆分為多列儲存;
訂單資訊:
id訂單日期訂單金額商品資訊
12017-10-0110001,2,3,4
2).為每個表建立一個"主鍵"欄位。
3).要保證每個表只記錄一個事情,如果記錄了多件事情,應該分表儲存;
學員資訊表:
id姓名性別年齡科目(科目需要被分離出來,單獨做表)
1張三男22JavaEE
2李四女18ISO
02.MySQL多表_實戰_一對多關係:
1).客戶和訂單:
1).主從關係:
主表:客戶
從表:訂單
2).主表:客戶表:
id客戶名稱註冊日期等級
   從表:訂單表:
id訂單日期訂單金額客戶ID
2).部門和員工:
1).主從關係:
主表:部門
從表:員工
2).主表:部門表
id部門名稱建立日期
   從表:員工表
id姓名性別入職日期薪水部門ID(外來鍵)
3).省和市:
1).主從關係:
主表:省
從表:市
2).主表:省
idname
   從表:市
idname省ID
3).自連線實現:
idnameparentId
1北京NULL
2河北NULL
3河南NULL
4北京1
5廊坊2
6石家莊2
7鄭州3
8洛陽3
03.MySQL多表_實戰_多對多關係:
1).學生和課程:(前提:一個學生可以選修多門課程)
1).主表:學員表:
id姓名性別年齡
1zhangsan 男20
   主表:課程表:
id課程名稱課時
   從表:學員_課程表(關係表)
學員ID課程ID
11
12
13
21
22
2).使用者和角色:
1).主表:使用者表:
id姓名
   主表:角色表
id角色名
   從表:使用者_角色(關係表)
使用者ID角色ID
11
12
21
32
04.MySQL多表_查詢_內連線查詢

0).查詢結果:(也叫:等值連線查詢)可以查詢出兩個關聯表中的"等值部分"的記錄。【最常用的】
1).隱式內連線:select * from 表1 , 表2 where 表1和表2的等值關係;
1).需求:查詢所有的商品並顯示類別名稱
select * from products,category where products.category_id = category.cid;
2).顯示內連線:select * from 表1 INNER JOIN 表2 ON 兩個表的等值關係;
select * from products inner join category on products.category_id = category.cid;
3).注意事項:
1).隱式內連線:select * from 表1,表2 ON 兩個表的等值關係;//錯誤
2).顯示內連線:select * from 表1 inner join 表2 where 兩個表的等值關係;//OK的--(但不建議,及其的不規範)
3).增加條件語句:
隱式內連線:select .. from 表1,表2 where 兩個表的等值關係 and 其它條件
顯示內連線:select .. from 表1 inner join 表2 on 兩個表的等值關係 where 其它條件;
4).使用表別名:
例如下面語句查詢商品大於2000元的商品,並且顯示類別名稱:
SELECT * FROM products , category WHERE products.category_id = category.cid AND products.price > 2000;
1).products.category_id:前面的"表名"的限定不是必須的,前提是兩個表沒有"同名欄位"。如果要使用兩個表中的"同名欄位"就需要使用"表名"字首。
2).可以為表起個別名,使用起來可以方便些:
隱式內連線:
select * from products p , category c where p.cid = c.cid and p.price > 2000;
顯示內連線:
select * from products p inner join category c on p.cid = c.cid where p.price > 2000;

   注意:使用別名後,後面不能再使用表全名了。

                5).關於篩選欄位:

多表連線查詢的結果欄位,可以來自於兩個表:
商品名商品價格   類別名稱
select p.pname,p.price,c.cname from products p , category c where p.cid = c.cid;
05.MySQL多表_查詢_外連線查詢
1).左外連線查詢:select ... from 表1 LEFT JOIN 表2 ON 兩個表的等值關係;
需求:查詢所有的商品資訊,有類別的要顯示類別名稱。
select * from products p LEFT JOIN category c on p.cid = c.cid;
查詢結果:可以查詢出"左表"的所有記錄,以及"右表"的等值記錄。
2).右外連線查詢:
一樣的需求:查詢所有的商品資訊,有類別的要顯示類別名稱。
select * from category c RIGHT JOIN products p on p.cid = c.cid;
查詢結果:可以查詢出"右表"的所有記錄,以及"左表"的等值記錄。
06.MySQL多表_查詢_練習:
1).查詢出所有的"服飾"類商品的:商品名稱,價格,類別名稱。要求結果按價格升序排序;
select p.pname,p.price,c.cname from products p , category c where p.cid = c.cid and c.cname = '服飾' order by p.price asc;
2).查詢價格高於2000元的商品,要求同時顯示商品類別名稱。
select * from products p, category c where p.cid = c.cid and p.price > 2000;
07.MySQL多表_三種查詢的結果示意圖:沒有(^_^)
08.MySQL多表_三表聯查:
學員表:
id姓名
科目表:
id名稱
學員_科目(關係表)
學員ID科目ID
1).內連線:
1).隱式內連線:
select ... from 表1 , 關係表 , 表2 
where 表1 和 "關係表"的等值關係 and 表2 和"關係表"的等值關係;
2).顯示內連線:
select ... from 表1 INNER JOIN 關係表 ON 表1和"關係表"的等值關係 INNER JOIN 表2 ON "關係表"和"表2"的等值關係;
2).外連線:
1).左外連線:
select ... from 表1 left join 關係表 on 等值關係 left join 表2 on 關係表和表2的等值關係;
2).右外連線:
select ... from 表1 right join 關係表 on 等值關係 right join 表2 on 關係表和表2的等值關係;
09.MySQL子查詢:
1).需求:查詢出價格高於"勁霸"的商品資訊
select * from products where price > (select price from products where pname = '勁霸');
2).寫在一個查詢內部的查詢就叫:子查詢。
3).子查詢通常作為外部查詢的:條件,虛擬表來使用;
A).作為條件:
示例:查詢出價格高於"勁霸"的商品資訊
      select * from products where price > (select price from products where pname = '勁霸');
      查詢所有的"家電"類和"服飾"類的商品資訊(只要獲取商品資訊即可)
      select * from products where cid = 'c001' or cid = 'c002';
      select * from products where cid in (select cid from category where cname in('家電','服飾'));
B).作為外部查詢的虛擬表:
示例:查詢所有的"服飾"類商品
正常兩個表聯查:select * from products p , category c where p.cid = c.cid and c.name = '服飾';
使用子查詢:    select * from products p , (select * from category where name= '服飾') c where p.cid = c.cid;
4).練習:
A).查詢"化妝品"分類上架商品詳情:
a).正常使用兩個表聯查:
select * from products p, category c where p.cid = c.cid and c.cname = '化妝品' and p.flag = 1;
b).使用子查詢兩個關聯查詢:
select * from products p , (select * from category where cname = '化妝品') c where p.cid = c.cid and p.flag = 1;
c).單表查詢,只查商品表:
select * from products where cid = (select cid from category where cname = '化妝品') and flag = 1;
=========================黃金分割線===========================================

總結:
1,使用內連線進行多表查詢:查詢結果:兩個表中的"等值部分的記錄"
a,內連線的兩種查詢方式
1).隱式內連線
2).顯示內連線
b,顯式內連線的SQL語句
select ... from 表1 INNER JOIN 表2 ON 兩個表的等值條件;
c,隱式內連線的SQL語句
select ... from 表1 , 表2 where 兩個表的等值條件;
2,能夠使用外連線進行多表查詢
a,外連線的兩種查詢方式
1).左外連線:查詢結果:左表的所有記錄(等值的+不等值的) 和 右表的等值記錄。
2).右外連線:查詢結果:右表的所有記錄(等值的+不等值的) 和 左表的等值記錄。
b,左外連線的SQL語句
select ... from 表1 LEFT JOIN 表2 ON 兩個表的等值條件;
c,右外連線的SQL語句
select ... from 表1 RIGHT JOIN 表2 ON 兩個表的等值條件;
3,能夠使用子查詢進行多表查詢
查詢價格高於"勁霸"的商品資訊:
select * from products where price > (select price from products where pname = '勁霸');