首頁  >  文章  >  資料庫  >  mysql中的多表聯合查詢語句是什麼

mysql中的多表聯合查詢語句是什麼

coldplay.xixi
coldplay.xixi原創
2020-09-03 14:01:2511412瀏覽

mysql中的多表聯合查詢語句是:【select 語句1 union [union 選項] select 語句2 union [union 選項] select 語句n】。多表聯合查詢結果是將多個select語句的查詢結果結合在一起。

mysql中的多表聯合查詢語句是什麼

【相關學習推薦:#mysql教學##(影片)】

mysql多表聯合查詢語句是:

聯合查詢結果是將多個select語句的查詢結果結合在一起。

可以使用union和union all關鍵字進行合併。

基本語法:

select 語句1

union [union 選項]

select 語句2

union [union選項]

select 語句n

其中union選項有兩個選項可選:all(表示重複也輸出);distinct(去重,完全重複的,預設會去重)

兩個表的欄位一致即可。

例:
select id,addrid 
from addr 
union all 
select id,addrid 
from student

聯合查詢的意義

1.查詢同一張表,但是需求不同

2.多表查詢:多張表的結構完全一樣,保存的資料(結構)也是一樣的

聯合查詢order by的使用

#在聯合查詢中:order by只能最後使用一個,需要對查詢語句用括號才行。

例:
---(错误)
select * from student where sex="man" order by score
union
select * from student wherre sex="woman" order by score;
这种情况会报错,因为一个句子中不能有两个order by
---(正确但不符合所需)
select * from student where sex="man" 
union
select * from student wherre sex="woman" order by score;
这种情况是正确的,但是合并又没有意义,他会把之前的sex分好的情况给打乱
---(正确)
(select * from student where sex="man" order by score 
limit 10)
union
(select * from student wherre sex="woman" order by score
limit 10);
在子语句中使用order by,由于优先级的问题,需要将整个子句用()括起来,且必须和limit结合使用,否则不会生效。

想了解更多程式設計學習,請關注

php培訓欄位!

#

以上是mysql中的多表聯合查詢語句是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn