1.子查詢的使用
- 進行過濾,試着將下列三個查詢select語句組合爲1條嵌套查詢語句:
select order_num from orderitems where prod_id='tnt2';
select cust_id from orders where order_num in(20005,20007);//兩個數字爲上面的sql語句檢索結果
select cust_name,cust_contact from customers where cust_id in(10001,10004);//兩個數字爲上面的sql語句檢索結果
組合之後:
mysql> select cust_name,cust_contact from customers where cust_id in(
-> select cust_id from orders where order_num in(
-> select order_num from orderitems where prod_id='tnt2'));
多個表組合求解,得出給定物品購買者的用戶信息。先從物品列表找到包含該物品的訂單號,然後在對應的訂單裏面找到客戶ID,最後在客戶表中找到cust_id對應的客戶信息。
- 作爲計算字段使用子查詢
如題,考慮當使用子查詢語句作爲計算字段時如何設計sql語句,見下實例:
mysql> select cust_name,cust_contact,(select count(*) from orders where customers.cust_id=orders.cust_id)
-> as orders from customers order by cust_name;
2.內部聯結表
聯結方式 | 示例 |
---|---|
WHERE子句 | select vend_name,prod_name,prod_price from vendors,products where vendors.vend_id=products.vend_id; |
顯式指定 | select vend_name,prod_name,prod_price from vendors INNER JOIN products ON vendors.vend_id=products.vend_id; |
使用多個表 | select cust_name from customers... where ... AND orders.order_num=orderitems.order_num AND prod_id='tnt2'; |
3.創建高級聯結
聯結類型 | 作用 |
---|---|
自聯結 | 用來替換在相同表中檢索數據時使用的子查詢語句 |
自然聯結 | 保證每個列只是出現一次,要靠自己保證,基本所有內部聯結都是自然聯結 |
外部聯結 | 用於解鎖在兩個表中沒有關聯關係的行,即表1中的主鍵找不到表2中對應的外鍵 |
- 自聯結
select prod_id,prod_name from products where vend_id=
(select vend_id from products where prod_id='dtntr');//子查詢方式
select p1.prod_id,p1.prod_name from products as p1,products as p2 where
p1.vend_id=p2.vend_id AND p2.prod_id='dtntr';//自聯結方式,使用別名更加簡單
- 外部聯結
常常配合LEFT與RIGHT關鍵字使用指定OUTER JOIN左邊還是右邊的表爲包括其所有行的表:
//下面要求檢索所有客戶的訂單號包括那些還沒有訂單的客戶
select customers.cust_id,orders.order_num from customers INNER JOIN
orders ON customers.cust_id=orders.cust_id;//內部聯結,只能檢索有訂單的客戶
select customers.cust_id,orders.order_num from customers LEFT OUTER JOIN
orders ON customers.cust_id=orders.cust_id; //外部聯結,可滿足題意檢索所有客戶
4.組合查詢
查詢方式 | 示例(所有語句等價) |
---|---|
UNION並 | select vend_id,prod_price from products where prod_price <= 5 UNION select vend_id,prod_price from products where vend_id in(1001,1002); |
WHERE子句 | select vend_id,prod_price from products where prod_price<=5 OR vend_id in(1001,1002); |
UNION ALL | UNION默認剔除重複行,而加上ALL之後不剔除重複行 |
排序 | 在union中order by子句只能出現在最後面,如:…order by vend_id; |
5.全文本搜索
搜索方法 | 示例 |
---|---|
match,against | match指定被搜索的列,against指定要使用的搜索表達式 |
全文本搜索 | select note_text from productnotes where match(note_text) against(‘rabbit’); |
LIKE語句 | select note_text from productnotes where note_text LIKE ‘%rabbit%’; |
查詢擴展(with …) | select note_text from productnotes where match(note_text) against(‘rabbit’ with query expansion); |
布爾文本搜索 | 示例 |
---|---|
- | select note_text from productnotes where match(note_text) against(‘heavy -rope*’ in boolean mode); |
+ | select note_text from productnotes where match(note_text) against(’+rabbit +bait’ in boolean mode); |
匹配任意一個 | select note_text from productnotes where match(note_text) against(‘rabbit bait’ in boolean mode); |
匹配整個詞 | select note_text from productnotes where match(note_text) against(’“rabbit bait”’ in boolean mode); |
> | 包含,增加等級值,如:… against(’>rabbit’ in boolean mode); |
< | 包含,降低等級值,如:against(’<carrot’ in boolean mode); |
~ | 取消一個詞的排序值 |
* | 詞尾通配符 |
注意:全文本搜索會按找等級值進行排序,而LIKE不會