.net 获取字符串中的第一个逗号的位置_SQLZOO中做错过的题
第2關(guān)——簡單查詢
case......end表達(dá)式
更正下面鏈接中做錯過的題
SQLZOO:SELECT from WORLD Tutorial/zh?sqlzoo.net參考答案鏈接地址:
第2關(guān)《從零學(xué)會SQL:簡單查詢》練習(xí)題答案?mp.weixin.qq.com11題
SELECT name, ( CASE WHEN continent='Oceania' THEN 'Australasia'ELSE continent END ) FROM world WHERE name LIKE 'N%';12題
select name, (case when continent in ('Europe', 'Asia') then 'Eurasia' when continent in ('North America', 'South America', 'Caribbean') then 'America' else continent end) as continent1 from world where name like 'A%' or name like 'B%';13題
select name, continent, ( case when continent='Oceania' then 'Australasia' when continent='Eurasia' then 'Europe/Asia' when name='Turkey' then 'Europe/Asia' when continent='Caribbean' and name like 'B%' then 'North America' when continent='Caribbean' and name not like 'B%' then 'South America' else continent end ) as a from world order by name;字符串模糊查詢
更正下面鏈接中做錯過的題
SELECT names/zh - SQLZOO?sqlzoo.net參考答案鏈接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762555&idx=1&sn=1c57656162a01163135652fafc120e32&scene=19#wechat_redirect?mp.weixin.qq.com11題
SELECT name FROM world WHERE name = capital;12題
SELECT name FROM world WHERE capital = concat(name, ' City');上面的代碼中,字符串City前有空格
13題
select capital, name from world where capital like concat('%',name,'%');14題
SELECT name, capital FROM world WHERE capital LIKE concat('%',name,'_%');15題
select name,replace(capital, name, '') from world where capital Like concat(name,'%_%');第3關(guān)——匯總分析
簡單查詢
更正下面鏈接中做錯過的題
SELECT from Nobel Tutorial/zh?sqlzoo.net參考答案鏈接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762573&idx=1&sn=3e4d24bc8c23f7d44d0473f074407e4c&scene=19#wechat_redirect?mp.weixin.qq.com5題
/* 【知識點】比較運算符,邏輯運算符(and),括號里的有優(yōu)先級 */ select yr, subject, winner from nobel where (yr between 1980 and 1989) and subject = 'Literature';8題
/* 【知識點】比較運算符,邏輯運算符(or) */ select yr, subject, winner from nobel where ( yr = 1980 and subject = 'physics' ) or ( yr = 1984 and subject = 'chemistry' );12題
/* 跳脫字符:單引號 你不能把一個單引號直接的放在字符串中。但您可連續(xù)使用兩個單引號在字符串中當(dāng)做一個單引號。 */ select * from nobel where winner = 'EUGENE O''NEILL';13題
select winner, yr, subject from nobel where winner like 'Sir%' order by yr desc, winner;14題
/* 14.查找1984年獲獎?wù)吆椭黝}按主題和獲勝者名稱排序, 并把化學(xué)獎和物理獎排到最后面顯示 之前sqlzoo網(wǎng)站這個是可以y運行正確的,但是現(xiàn)在網(wǎng)站可能出了問題,無法運行正確 理解這個sql就可以了 【知識點】運算符in */select winner, subject from nobel where yr=1984 order by subject in ('Physics','Chemistry'),subject,winner;sql解釋: subject in ('Physics','Chemistry')返回值(0或者1), 會對每一個subject做一個if的判斷,有的是1,沒有的是0 再用order by把這些值排序在下面這兩個科目('Physics','Chemistry')的就是0排在前邊, 是這兩個科目的返回1就排在后邊了。 因為化學(xué)和物理科目題目要求在后面,所以引入此函數(shù)出現(xiàn)0、1,達(dá)成題目的要求第3關(guān)——匯總分析
匯總分析
更正下面鏈接中做錯過的題
SUM and COUNT/zh?sqlzoo.net參考答案鏈接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762573&idx=1&sn=3e4d24bc8c23f7d44d0473f074407e4c&scene=19#wechat_redirect?mp.weixin.qq.com2題
select continent from world group by continent;7題
錯誤做法:
select continent, count(name) from world group by continent having population > 10000000;錯誤原因:
group by產(chǎn)生“臨時表” 后,再對臨時表進(jìn)行 having過濾。此題中,world這個表按continent進(jìn)行g(shù)roup by 后,population要用聚合函數(shù)來處理,因為一個continent里多個國家,也就有多個population,沒用聚合函數(shù),它就不知道你指的是哪個population。
正確做法:
select continent,count(name) from world where population>=10000000 group by continent;錯題總結(jié):
group by分組之后的SQL執(zhí)行語句中,必須使用聚合函數(shù);
但有時不分組,卻可以使用聚合函數(shù),如下題:
select count( name ) from world where area > 1000000;第4關(guān)——復(fù)雜查詢
子查詢(標(biāo)量、關(guān)聯(lián))
子查詢(標(biāo)量、關(guān)聯(lián))用括號框住,再進(jìn)行算術(shù)、比較、邏輯運算。
更正下面鏈接中做錯過的題
SELECT within SELECT Tutorial/zh?sqlzoo.net參考答案鏈接地址:
https://mp.weixin.qq.com/s?__biz=MzAxMTMwNTMxMQ==&mid=501762673&idx=1&sn=d30b58f8c826c145c81bc0309452f6ef&scene=19#wechat_redirect?mp.weixin.qq.com3題
SELECT NAME,continent FROMworld WHEREcontinent IN (( SELECT continent FROM world WHERE NAME = 'Argentina' ),( SELECT continent FROM world WHERE NAME = 'Australia' )) ORDER BY NAME ASC; # in 后面的括號中用逗號隔開單行單列的標(biāo)量另法:
select name, continent from world where continent in ( select continent from world where name='Argentina' or name='Australia') order by name; # 此解法中 in 后面的括號中的參數(shù)是多行單列的值 # 若子查詢的結(jié)果是多行單列,結(jié)果集類似一個列,父查詢使用IN運算符5題
/* concat函數(shù)實現(xiàn)字符串拼接,第一個參數(shù)不要求是字符串類型,第二個參數(shù)要求是字符串類型 round函數(shù)實現(xiàn)第一個數(shù)值類型參數(shù)的四舍五入,第二個參數(shù)(正數(shù))代表精確到小數(shù)點后第幾位 */select name, concat(round(population /(select population from world where name='Germany')*100,0),'%') from world where continent = 'Europe';6題
select name from world where gdp > all( select gdp from world where continent = 'Europe' and gdp > 0 );# gdp 中有空值,要增加篩選條件 gdp > 07題
錯誤做法:
SELECT continent,name ,max( area ) FROM world where area > 0 group by continent;# 只有當(dāng)某些字段單獨分組時得到的結(jié)果一樣時,才能聯(lián)合分組 # 聯(lián)合分組的好處是使select子句的字段不用聚合函數(shù),以此回避不能對字段使用聚合函數(shù)的情況SELECT continent, name ,max( area ) FROM world where area > 0 group by continent, name;# 分組之后,非分組列的列名全部要用聚合函數(shù),否則報錯SELECT continent,count( name ) ,max( area ) FROM world where area > 0 group by continent;正確做法:
/* 【知識點】關(guān)聯(lián)子查詢 一般來說 先執(zhí)行子查詢,但關(guān)聯(lián)子查詢例外。有關(guān)聯(lián)子查詢時,先執(zhí)行主查詢再執(zhí)行關(guān)聯(lián)子查詢。 */SELECTcontinent,NAME,area FROMworld AS x WHEREarea = ( SELECT max( area ) FROM world AS y WHERE x.continent = y.continent );# 兩張臨時表x,y通過字段continent進(jìn)行關(guān)聯(lián)8題
SELECTcontinent, NAME FROMworld AS x WHERENAME <= ALL ( SELECT NAME FROM world AS y WHERE y.continent = x.continent );9題
SELECT NAME,continent,population FROMworld AS x WHERE25000000 >= ALL ( SELECT population FROM world AS y WHERE y.continent = x.continent );10題
錯誤做法:
SELECT NAME,continent FROMworld AS x WHEREpopulation >= 3 * ALL ( SELECT population FROM world AS y WHERE x.continent = y.continent );正確做法:
SELECT NAME,continent FROMworld AS x WHEREpopulation > ALL ( SELECT 3 * population FROM world AS y WHERE y.continent = x.continent AND x.NAME <> y.NAME );第5關(guān)——多表查詢
多表查詢
更正下面鏈接中做錯過的題
The JOIN operation/zh?sqlzoo.net參考答案鏈接地址:
第5關(guān)《從零學(xué)會SQL:多表查詢》練習(xí)題答案?mp.weixin.qq.com8題
/* 問題分析: 1)查找進(jìn)球球員姓名,對戰(zhàn)雙方2)條件:有一個球隊是德國(球隊編號'GER'),被射入了球 */SELECT DISTINCT b.player FROMgame AS a INNER JOIN goal AS b ON a.id = b.matchid WHERE( b.teamid = a.team1 AND a.team2 = 'GER' ) OR ( b.teamid = a.team2 AND a.team1 = 'GER');/* 1)解釋下where子句中的條件: 主隊是德國或者客隊是德國,比如 德國 和 A對比賽(德國是主隊),進(jìn)球的是A隊 A隊和德國比賽(德國是客隊),進(jìn)球的是A隊 所以條件是(b.teamid = a.team1 and a.team2 = 'GER') or (b.teamid = a.team2 and a.team1 = 'GER')2)內(nèi)聯(lián)結(jié)的結(jié)果中入門球員有重復(fù)值,用distinct去掉重復(fù)值 */9題
select c.teamname, count(c.teamname) from eteam as c inner join goal as b on c.id=b.teamid group by c.teamname;group by分組之后的SQL執(zhí)行語句中,非分組列的列名全部要用聚合函數(shù),否則報錯;但有時不分組,卻可以使用聚合函數(shù)
11題
錯誤解法:
select a.id,a.mdate, count(b.player) from game as a inner join goal as b on a.id = b.matchid where (team1 = 'POL' or team2 = 'POL') group by a.id;錯誤原因:
group by分組之后的SQL執(zhí)行語句中,非分組列的列名全部要用聚合函數(shù),否則報錯;但有時不分組,卻可以使用聚合函數(shù)
正確解法一:
利用聯(lián)合分組,使select子句的字段不用聚合函數(shù),以此回避不能對字段使用聚合函數(shù)的情況
# 只有當(dāng)某些字段單獨分組時得到的結(jié)果一樣時,才能聯(lián)合分組 # 聯(lián)合分組的好處是使select子句的字段不用聚合函數(shù),以此回避不能對字段使用聚合函數(shù)的情況select a.id,a.mdate, count(b.player) from game as a inner join goal as b on a.id = b.matchid where (team1 = 'POL' or team2 = 'POL') group by a.id, a.mdate;正確解法二:
利用子查詢與多表連接,將需要用的字段從少到多一一串起來
SELECT x.`賽事編號`, y.mdate, x.`入球數(shù)字` FROM ( SELECTa.id as `賽事編號`,count( b.player ) as `入球數(shù)字` FROMgame AS aINNER JOIN goal AS b ON a.id = b.matchid WHERE( team1 = 'POL' OR team2 = 'POL' ) GROUP BYa.id ) as x INNER JOIN game AS y ON x.`賽事編號` = y.id;12題
# 只有當(dāng)某些字段單獨分組時得到的結(jié)果一樣時,才能聯(lián)合分組 # 聯(lián)合分組的好處是使select子句的字段不用聚合函數(shù),以此回避不能對字段使用聚合函數(shù)的情況select a.id,a.mdate,count(player) from game as a inner join goal as b on a.id=b.matchid where b.teamid='GER' group by a.id,a.mdate;13題
# 只有當(dāng)某些字段單獨分組時得到的結(jié)果一樣時,才能聯(lián)合分組 # 聯(lián)合分組的好處是使select子句的字段不用聚合函數(shù),以此回避不能對字段使用聚合函數(shù)的情況SELECTa.mdate,a.team1,sum( CASE WHEN a.team1 = b.teamid THEN 1 ELSE 0 END ) AS score1,a.team2,sum( CASE WHEN a.team2 = b.teamid THEN 1 ELSE 0 END ) score2 FROMgame AS aLEFT JOIN goal AS b ON a.id = b.matchid GROUP BYa.id,a.mdate,a.team1,a.team2 ORDER BYa.mdate,a.id,a.team1,a.team2;總結(jié)
以上是生活随笔為你收集整理的.net 获取字符串中的第一个逗号的位置_SQLZOO中做错过的题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: atm系统的用例模型_战斗系统执行式测试
- 下一篇: jtoken判断是否包含键_Redis列