.net 获取字符串中的第一个逗号的位置_SQLZOO中做错过的题
第2關——簡單查詢
case......end表達式
更正下面鏈接中做錯過的題
SQLZOO:SELECT from WORLD Tutorial/zh?sqlzoo.net參考答案鏈接地址:
第2關《從零學會SQL:簡單查詢》練習題答案?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關——匯總分析
簡單查詢
更正下面鏈接中做錯過的題
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),括號里的有優先級 */ 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題
/* 跳脫字符:單引號 你不能把一個單引號直接的放在字符串中。但您可連續使用兩個單引號在字符串中當做一個單引號。 */ 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年獲獎者和主題按主題和獲勝者名稱排序, 并把化學獎和物理獎排到最后面顯示 之前sqlzoo網站這個是可以y運行正確的,但是現在網站可能出了問題,無法運行正確 理解這個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就排在后邊了。 因為化學和物理科目題目要求在后面,所以引入此函數出現0、1,達成題目的要求第3關——匯總分析
匯總分析
更正下面鏈接中做錯過的題
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產生“臨時表” 后,再對臨時表進行 having過濾。此題中,world這個表按continent進行group by 后,population要用聚合函數來處理,因為一個continent里多個國家,也就有多個population,沒用聚合函數,它就不知道你指的是哪個population。
正確做法:
select continent,count(name) from world where population>=10000000 group by continent;錯題總結:
group by分組之后的SQL執行語句中,必須使用聚合函數;
但有時不分組,卻可以使用聚合函數,如下題:
select count( name ) from world where area > 1000000;第4關——復雜查詢
子查詢(標量、關聯)
子查詢(標量、關聯)用括號框住,再進行算術、比較、邏輯運算。
更正下面鏈接中做錯過的題
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 后面的括號中用逗號隔開單行單列的標量另法:
select name, continent from world where continent in ( select continent from world where name='Argentina' or name='Australia') order by name; # 此解法中 in 后面的括號中的參數是多行單列的值 # 若子查詢的結果是多行單列,結果集類似一個列,父查詢使用IN運算符5題
/* concat函數實現字符串拼接,第一個參數不要求是字符串類型,第二個參數要求是字符串類型 round函數實現第一個數值類型參數的四舍五入,第二個參數(正數)代表精確到小數點后第幾位 */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;# 只有當某些字段單獨分組時得到的結果一樣時,才能聯合分組 # 聯合分組的好處是使select子句的字段不用聚合函數,以此回避不能對字段使用聚合函數的情況SELECT continent, name ,max( area ) FROM world where area > 0 group by continent, name;# 分組之后,非分組列的列名全部要用聚合函數,否則報錯SELECT continent,count( name ) ,max( area ) FROM world where area > 0 group by continent;正確做法:
/* 【知識點】關聯子查詢 一般來說 先執行子查詢,但關聯子查詢例外。有關聯子查詢時,先執行主查詢再執行關聯子查詢。 */SELECTcontinent,NAME,area FROMworld AS x WHEREarea = ( SELECT max( area ) FROM world AS y WHERE x.continent = y.continent );# 兩張臨時表x,y通過字段continent進行關聯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關——多表查詢
多表查詢
更正下面鏈接中做錯過的題
The JOIN operation/zh?sqlzoo.net參考答案鏈接地址:
第5關《從零學會SQL:多表查詢》練習題答案?mp.weixin.qq.com8題
/* 問題分析: 1)查找進球球員姓名,對戰雙方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對比賽(德國是主隊),進球的是A隊 A隊和德國比賽(德國是客隊),進球的是A隊 所以條件是(b.teamid = a.team1 and a.team2 = 'GER') or (b.teamid = a.team2 and a.team1 = 'GER')2)內聯結的結果中入門球員有重復值,用distinct去掉重復值 */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執行語句中,非分組列的列名全部要用聚合函數,否則報錯;但有時不分組,卻可以使用聚合函數
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執行語句中,非分組列的列名全部要用聚合函數,否則報錯;但有時不分組,卻可以使用聚合函數
正確解法一:
利用聯合分組,使select子句的字段不用聚合函數,以此回避不能對字段使用聚合函數的情況
# 只有當某些字段單獨分組時得到的結果一樣時,才能聯合分組 # 聯合分組的好處是使select子句的字段不用聚合函數,以此回避不能對字段使用聚合函數的情況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.`入球數字` FROM ( SELECTa.id as `賽事編號`,count( b.player ) as `入球數字` 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題
# 只有當某些字段單獨分組時得到的結果一樣時,才能聯合分組 # 聯合分組的好處是使select子句的字段不用聚合函數,以此回避不能對字段使用聚合函數的情況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題
# 只有當某些字段單獨分組時得到的結果一樣時,才能聯合分組 # 聯合分組的好處是使select子句的字段不用聚合函數,以此回避不能對字段使用聚合函數的情況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;總結
以上是生活随笔為你收集整理的.net 获取字符串中的第一个逗号的位置_SQLZOO中做错过的题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: atm系统的用例模型_战斗系统执行式测试
- 下一篇: dwcs6怎么添加搜索框_一文教会你微信