第1關(guān) 交換工資
一、本關(guān)任務(wù):給定一張?tb_Salary
?表,如下所示,有 m = 男性
和 f = 女性
的值。交換所有的 f
和 m
值(例如,將所有 f
值更改為 m
,反之亦然)。
id | name | sex | salary |
---|---|---|---|
1 | Elon | f | 7000 |
2 | Donny | f | 8000 |
3 | Carey | m | 6000 |
4 | Karin | f | 9000 |
5 | Larisa | m | 5500 |
6 | Sora | m | 500 |
要求只使用一句更新update
語(yǔ)句,且不允許含有任何select
語(yǔ)句完成任務(wù)。
二、編程要求
根據(jù)提示在Begin - End
區(qū)域內(nèi)進(jìn)行代碼補(bǔ)充。
提示
可能需要使用到 CASE 函數(shù)或 IF 函數(shù),使用方法如下實(shí)例:
SELECT
case ###如果
when sex='1' then '男' ###sex='1',則返回值'男'
when sex='2' then '女' ###sex='2',則返回值'女'
else '其他' ###其他的返回'其他’
end ###結(jié)束
from sys_user ###整體理解: 在sys_user表中如果sex='1',則返回值'男'如果sex='2',則返回值'女' 否則返回'其他’
select if(sex='1','男','女') as sex from sys_user; ###如果sex='1'則返回值'男' 否則返回值為'女'
三、預(yù)期輸出:
+----+--------+-----+--------+
| id | name? | sex | salary |
+----+--------+-----+--------+
| 1?| Elon? | m? | 7000? |
| 2?| Donny?| m? | 8000? |
| 3?| Carey?| f? | 6000? |
| 4?| karin?| m? | 9000? |
| 5?| Larisa | f? | 5500? |
| 6?| Sora? | f? | 500??|
+----+--------+-----+--------+
?四、代碼
########## Begin ##########
update tb_Salary set sex = if(sex = 'f','m','f') where id in(1,2,3,4,5,6);
########## End ##########
第2關(guān) 換座位
一、本關(guān)任務(wù):改變相鄰倆學(xué)生的座位。
小美是一所中學(xué)的信息科技老師,她有一張 tb_Seat
座位表,平時(shí)用來(lái)儲(chǔ)存學(xué)生名字和與他們相對(duì)應(yīng)的座位 id
。
tb_Seat
表結(jié)構(gòu)數(shù)據(jù)如下:
id | name |
---|---|
1 | Elon |
2 | Donny |
3 | Carey |
4 | Karin |
5 | Larisa |
現(xiàn)在小美想改變相鄰倆學(xué)生的座位(若學(xué)生人數(shù)為奇數(shù),則無(wú)需改變最后一位同學(xué)的座位),現(xiàn)在需要你編寫(xiě)SQL
輸出小美想要的的結(jié)果。
二、編程要求
根據(jù)提示,在Begin - End
區(qū)域內(nèi)進(jìn)行代碼補(bǔ)充。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-407809.html
三、預(yù)期輸出:
id name
1 Donny
2 Elon
3 Karin
4 Carey
5 Larisa
?四、代碼
########## Begin ##########
select
case id
when 1 then 2
when 2 then 1
when 3 then 4
when 4 then 3
when 5 then 5
else 0
end id,name from tb_Seat order by id asc;
########## End ##########
第3關(guān) 分?jǐn)?shù)排名
一、本關(guān)任務(wù):編寫(xiě)SQL
查詢來(lái)實(shí)現(xiàn)二種排名方式的分?jǐn)?shù)排名。
score
表結(jié)構(gòu)信息如下:
Id | Score |
---|---|
1 | 3.52 |
2 | 3.65 |
3 | 4.23 |
4 | 3.85 |
5 | 4.23 |
6 | 3.65 |
如果兩個(gè)分?jǐn)?shù)相同,則兩個(gè)分?jǐn)?shù)排名(Rank
)相同。
情況一:平分后的下一個(gè)名次是下一個(gè)連續(xù)的整數(shù)值。換句話說(shuō),名次之間不應(yīng)該有“間隔”。例:1、1、2、3、4、4。
情況二:排名是非連續(xù)的。例:1、1、1、4、4、6。
二、編程要求
根據(jù)提示,在Begin - End
區(qū)域內(nèi)進(jìn)行代碼補(bǔ)充。
?三、代碼
########## Begin ##########
SELECT Score,(SELECT COUNT(DISTINCT Score) FROM score WHERE score>=s.score) AS Rank FROM score s ORDER BY score DESC;
SELECT Score,(SELECT COUNT(Score)+1 FROM score WHERE score>s.score) AS Rank FROM score s ORDER BY score DESC;
########## End ##########
第4關(guān) 體育館的人流量
一、本關(guān)任務(wù):某市建了一個(gè)新的體育館,每日人流量信息被記錄在gymnasium
表中:序號(hào) (id
)、日期 (date
)、?人流量 (visitors_flow
)。
請(qǐng)編寫(xiě)一個(gè)查詢語(yǔ)句,找出人流量處于高峰的記錄 id、日期 date 和人流量 visitors_flow,其中高峰定義為前后連續(xù)三天人流量均不少于 100。
gymnasium
表結(jié)構(gòu)數(shù)據(jù)如下:
id | date | visitors_flow |
---|---|---|
1 | 2019-01-01 | 58 |
2 | 2019-01-02 | 110 |
3 | 2019-01-03 | 123 |
4 | 2019-01-04 | 67 |
5 | 2019-01-05 | 168 |
6 | 2019-01-06 | 1352 |
7 | 2019-01-07 | 382 |
8 | 2019-01-08 | 326 |
9 | 2019-01-09 | 99 |
提示:每天只有一行記錄,日期隨著 id
的增加而增加。
二、編程要求
根據(jù)提示,在Begin - End
區(qū)域內(nèi)進(jìn)行代碼補(bǔ)充。
三、預(yù)期輸出:
+----+------------+---------------+
| id | date? ? | visitors_flow?|
+----+------------+---------------+
| 5?| 2019-01-05 | 168? ? ? ?|
| 6?| 2019-01-06 | 1352? ??? |
| 7?| 2019-01-07 | 382? ? ? ?|
| 8?| 2019-01-08 | 326? ? ? ?|
+----+------------+---------------+
?四、代碼
########## Begin ##########
select distinct a.*
from gymnasium a,gymnasium b,gymnasium c
where a.visitors_flow >= 100 and b.visitors_flow >= 100 and c.visitors_flow >= 100 and
(
(a.id+1 = b.id and b.id+1 = c.id) or
(a.id-1 = b.id and a.id+1 = c.id) or
(a.id-1 = c.id and a.id+1 = b.id) or
(a.id-2 = b.id and a.id-1 = c.id) or
(a.id-1 = b.id and a.id-2 = c.id)
)
order by a.id
########## End ##########
第5關(guān) 統(tǒng)計(jì)總成績(jī)
一、本關(guān)任務(wù):計(jì)算每個(gè)班的語(yǔ)文總成績(jī)和數(shù)學(xué)總成績(jī),其中低于 60 分的成績(jī)不記入總成績(jī)。
tb_score
結(jié)構(gòu)數(shù)據(jù):
name | chinese | maths |
---|---|---|
A | 89 | 98 |
B | 99 | 89 |
C | 55 | 66 |
D | 88 | 66 |
E | 55 | 66 |
F | 88 | 99 |
tb_class
表結(jié)構(gòu)數(shù)據(jù):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-407809.html
stuname | classname |
---|---|
A | C1 |
B | C2 |
C | C3 |
D | C2 |
E | C1 |
F | C3 |
二、編程要求
根據(jù)提示,在Begin - End
區(qū)域內(nèi)進(jìn)行代碼補(bǔ)充。
三、預(yù)期輸出:
+-----------+---------+-------+
| classname | chinese | maths |
+-----------+---------+-------+
| C1? ? ?| 89? ? | 164? |
| C2? ? ?| 187? ?| 155? |
| C3? ? ?| 88? ? | 165? |
+-----------+---------+-------+
?四、代碼
########## Begin ##########
select c.classname,
sum(case when chinese> 60 then chinese else 0 End) chinese,
sum(case when maths > 60 then maths else 0 end) maths
from tb_score s, tb_class c
where c.stuname=s.name
group by classname;
########## End ##########
到了這里,關(guān)于MySQL數(shù)據(jù)庫(kù)——復(fù)雜查詢(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!