《MySQL數(shù)據(jù)庫 —子查詢,聯(lián)合查詢》要點:
本文介紹了MySQL數(shù)據(jù)庫 —子查詢,聯(lián)合查詢,希望對您有用。如果有疑問,可以聯(lián)系我們。
導(dǎo)讀:一 使用IN關(guān)鍵字的子查詢1.查詢游戲類型是'棋牌類' 的游戲的分?jǐn)?shù)信息 游戲分?jǐn)?shù)表中并未包含游戲類型信息 思路一:采用鏈接查詢...
一 使用IN關(guān)鍵字的子查詢
1.查詢游戲類型是'棋牌類' 的游戲的分?jǐn)?shù)信息
??? 游戲分?jǐn)?shù)表中并未包含游戲類型信息
?? 思路一:采用鏈接查詢
?? 思路二: 分兩步進行,首先找到所以'棋牌類'游戲的編號,再以這一組編號為查詢依據(jù)完成查詢
?? select * from scores where gno in (select gno from games where gtype ='棋牌')
2.查詢沒有參與5號游戲的玩家QQ
select user_qq from users where user_qq not in (select user_qq from scores where gno=5)
二 使用exists 關(guān)鍵字的子查詢
1.如果存在昵稱為‘孫悟空’,則查詢分?jǐn)?shù)表中的數(shù)據(jù)
? select * from scores where exists (select * from users user_name ='孫悟空')
三 聯(lián)合查詢
select _statement union[all] select_statement [union[all] select_statement][...n]
作用與特點:可以把多條查詢語句所產(chǎn)生的結(jié)果集縱向連接為一體
???????????????? 有ALL關(guān)鍵字可以顯示全部數(shù)據(jù)(即重復(fù)的也顯示出來)
???????????????? 列的數(shù)量與類型都要兼容
select user_name from users
union
select gname from games
1.查詢玩家表中所有女性玩家和生日為空的玩家
select * from users where user_sex='女'
union
select * from users where user_birthday is null
<<=====>> select * from users where user_sex='女' or select * from users where user_birthday is null
2.查詢qq號是‘12302’的玩家所有分?jǐn)?shù)并計算出總分?jǐn)?shù)和平均分?jǐn)?shù),并顯示到同一結(jié)果集中
select user_qq,gno,score from scores where user_qq='12302' union all select '總分',' ',sum(score) from scores union all select '平均分',' ',avg(score) from scores where user_qq='12302'
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/5534.html