MySQL必知必會十七 組合查詢

2021-10-06 19:17:45 字數 3867 閱讀 8288

mysql允許執行多個查詢(多條select語句),並將結果作為單個查詢結果集返回。這些組合查詢通常稱為並( union) 或復合查詢(compound query)。

有兩種基本情況,其中需要使用組合查詢:

mysql>

select vend_id, prod_id, prod_price from products where prod_price <=

5union

select vend_id, prod_id, prod_price from products where vend_id in

(1001

,1002);

+---------+---------+------------+

| vend_id | prod_id | prod_price |

+---------+---------+------------+

|1003

| fc |

2.50||

1002

| fu1 |

3.42||

1003

| sling |

4.49||

1003

| tnt1 |

2.50||

1001

| anv01 |

5.99||

1001

| anv02 |

9.99||

1001

| anv03 |

14.99||

1002

| ol1 |

8.99|+

---------+---------+------------+

mysql>

select vend_id, prod_id, prod_price from products where prod_price <=

5or vend_id in

(1001

,1002);

+---------+---------+------------+

| vend_id | prod_id | prod_price |

+---------+---------+------------+

|1001

| anv01 |

5.99||

1001

| anv02 |

9.99||

1001

| anv03 |

14.99||

1003

| fc |

2.50||

1002

| fu1 |

3.42||

1002

| ol1 |

8.99||

1003

| sling |

4.49||

1003

| tnt1 |

2.50|+

---------+---------+------------+

在這個簡單的例子中,使用union可能比使用where子句更為複雜。但對於更複雜的過濾條件,或者從多個表(而不是單個表)中檢索資料的情形,使用union可能會使處理更簡單。

union從查詢結果集中自動去除了重複的行(換句話說,它的行為與單條select語句中使用多個where子句條件一樣)。

這是union的預設行為,但是如果需要,可以改變它。事實上,如果想返回所有匹配行,可使用union all而不是union。

mysql>

select vend_id, prod_id, prod_price from products where prod_price <=

5union

allselect vend_id, prod_id, prod_price from products where vend_id in

(1001

,1002);

+---------+---------+------------+

| vend_id | prod_id | prod_price |

+---------+---------+------------+

|1003

| fc |

2.50||

1002

| fu1 |

3.42||

1003

| sling |

4.49||

1003

| tnt1 |

2.50||

1001

| anv01 |

5.99||

1001

| anv02 |

9.99||

1001

| anv03 |

14.99||

1002

| fu1 |

3.42||

1002

| ol1 |

8.99|+

---------+---------+------------+

union與whereunion幾乎總是完成與多個where條件相同的工作。 union all為union的一種形式,它完成where子句完成不了的工作。如果確實需要每個條件的匹配行全部出現(包括重複行),則必須使用union all而不是where

select語句的輸出用order by子句排序。在用union組合查詢時,只能使用一條order by子句,它必須出現在最後一條select語句之後。對於結果集,不存在用一種方式排序一部分,而又用另一種方式排序另一部分的情況,因此不允許使用多條order by子句。

mysql>

select vend_id, prod_id, prod_price from products where prod_price <=

5union

allselect vend_id, prod_id, prod_price from products where vend_id in

(1001

,1002

)order

by vend_id, prod_price;

+---------+---------+------------+

| vend_id | prod_id | prod_price |

+---------+---------+------------+

|1001

| anv01 |

5.99||

1001

| anv02 |

9.99||

1001

| anv03 |

14.99||

1002

| fu1 |

3.42||

1002

| fu1 |

3.42||

1002

| ol1 |

8.99||

1003

| fc |

2.50||

1003

| tnt1 |

2.50||

1003

| sling |

4.49|+

---------+---------+------------+

組合不同的表為使表述比較簡單,本章例子中的組合查詢使用的均是相同的表。但是其中使用union的組合查詢可以應用不同的表。

MySQL必知必會 十四 組合查詢

開始線 mysql也允許執行多個查詢,並將結果作為單個查詢結果集返回,這些組合查詢通常稱為並或復合查詢 有兩種基本情況,其中需要使用組合查詢 1.在單個查詢中從不同的表返回類似結構的資料 2.對單個表執行多個查詢,按單個查詢返回資料 可用union操作符來組合數條sql查詢 查詢 小於等於5的所有物...

MySQL必知必會 組合查詢(Union)

php mysql sql 閱讀約 8 分鐘 本篇文章主要介紹使用union操作符將多個select查詢組合成乙個結果集。本文參考 mysql必知必會 工作實踐融合 在大多數開發中,使用一條select查詢就會返回乙個結果集。如果,我們想一次性查詢多條sql語句,並將每一條select查詢的結果合併...

mysql必知必會 子查詢 組合查詢

一.子查詢 巢狀在其他查詢中的查詢。二.利用子查詢進行過濾 表orders包含訂單號 客戶id 訂單日期的每個訂單 表orderitems儲存各個訂單的物品 表customers儲存客戶資訊。如 現在我們要檢索出訂購物品tnt2的所有客戶 1 檢索包含物品tnt2的所有訂單號 2 根據訂單號檢索出所...