.net 获取字符串中的第一个逗号的位置_SQLZOO中做错过的题

acbd1d597e0dd55713280bfc1d87ef54.png

第2关——简单查询

case......end表达式

更正下面链接中做错过的题

SQLZOO:SELECT from WORLD Tutorial/zh​sqlzoo.net

参考答案链接地址:

第2关《从零学会SQL:简单查询》练习题答案​mp.weixin.qq.com
121ca419315d55808f7ff46d2bf9bec4.png

11题

SELECT name,
( CASE 
  WHEN continent='Oceania' THEN 'Australasia'
  ELSE continent 
END )
FROM world
WHERE name LIKE 'N%';

cf8d89216cb16ddf21b67258e93da475.png

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%';

e6628f546ee326f8532382d23672c7cf.png

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.com

11题

SELECT name 
FROM world 
WHERE name = capital;

95d5c371686fac6aed1320629ee17536.png

12题

SELECT name  
FROM world 
WHERE capital = concat(name, ' City');

上面的代码中,字符串City前有空格

653dd24dcc54057852767d89cfdb89cf.png

13题

select capital, name 
from world 
where capital like concat('%',name,'%');

b8d52313b029ca68a495a72902750d2e.png

14题

SELECT name, capital 
FROM world 
WHERE capital LIKE concat('%',name,'_%');

e728d115894c0375a17442bdf1a309b1.png

15题

select name,replace(capital, name, '') 
from world 
where capital Like concat(name,'%_%');

7f0cc99a837cc16d463aa5d02c3b0020.png

第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.com

5题

/*
【知识点】比较运算符,逻辑运算符(and),括号里的有优先级
*/
select yr, subject, winner 
from nobel 
where (yr between 1980 and 1989) and subject = 'Literature';

a6a956f0bb265402e605b3ada5f247b2.png

8题

/*
【知识点】比较运算符,逻辑运算符(or)
*/
select yr, subject, winner 
from nobel 
where ( yr = 1980 and subject = 'physics' ) or ( yr = 1984 and subject = 'chemistry' );

f4f828cc90fca8c7fe8c6c5ddc65e16f.png

12题

/*
跳脫字符:单引号
你不能把一个单引号直接的放在字符串中。但您可连续使用两个单引号在字符串中当做一个单引号。
*/
select *
from nobel
where winner = 'EUGENE O''NEILL';

57f4fa2fb479534ac9cad0e1d26d396e.png

13题

select winner, yr, subject 
from nobel
where winner like 'Sir%'
order by yr desc, winner;

62234845b8c44d53ff1864c902e335e2.png

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,达成题目的要求

ec5f75bbef9c08169086bc31993c5265.png

第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.com

2题

select continent 
from world 
group by continent;

8577f7124b89efb674b90846d74e00be.png

7题

错误做法:

select continent, count(name)
from world
group by continent 
having population > 10000000;

d3aa6913777a58f042cb755da86e370a.png

错误原因:

group by产生“临时表” 后,再对临时表进行 having过滤。此题中,world这个表按continent进行group by 后,population要用聚合函数来处理,因为一个continent里多个国家,也就有多个population,没用聚合函数,它就不知道你指的是哪个population。

正确做法:

select continent,count(name) 
from world
where population>=10000000
group by continent;

85af64f975f7b468620fd1261f96ec55.png

错题总结:

group by分组之后的SQL执行语句中,必须使用聚合函数;

但有时不分组,却可以使用聚合函数,如下题:

select count( name ) 
from world
where area > 1000000;

78688e3e1175b3c742edacc7c3165231.png

第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.com

3题

SELECT 
  NAME,continent 
FROM
  world 
WHERE
  continent 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运算符

5743926579eab5b12e58eaa782e93bc0.png

5题

/*
concat函数实现字符串拼接,第一个参数不要求是字符串类型,第二个参数要求是字符串类型
round函数实现第一个数值类型参数的四舍五入,第二个参数(正数)代表精确到小数点后第几位
*/

select 
  name, concat(
               round(population /(select population from world where name='Germany')*100,0)
               ,'%'
               )
from world
where continent = 'Europe';

37bab6e7095ae466b8b6a7ceac235bc3.png

6题

select name
from world
where gdp > all( select gdp from world where continent = 'Europe' and gdp > 0 );

# gdp 中有空值,要增加筛选条件 gdp > 0

708c065a97731c99aba550bbef8e16cb.png

7题

错误做法:

SELECT  continent,name ,max( area ) 
FROM world
where area > 0
group by continent;

c6d2757a98e77bbd94a4fdec58926eb9.png
#  只有当某些字段单独分组时得到的结果一样时,才能联合分组
#  联合分组的好处是使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况

SELECT  continent, name ,max( area ) 
FROM world
where area > 0
group by continent, name;

9e068a39e780bd4ebad3a79ecf554582.png
# 分组之后,非分组列的列名全部要用聚合函数,否则报错

SELECT  continent,count( name ) ,max( area ) 
FROM world
where area > 0
group by continent;

2224a38b384e146fac4fd5a13cb8e09c.png

正确做法:

/*
【知识点】关联子查询
一般来说 先执行子查询,但关联子查询例外。有关联子查询时,先执行主查询再执行关联子查询。
*/

SELECT
	continent,
	NAME,
	area 
FROM
	world AS x 
WHERE
	area = ( SELECT max( area ) FROM world AS y WHERE x.continent = y.continent );

#  两张临时表x,y通过字段continent进行关联

7d617bdcb1c3e30b82e001169cd980c4.png

8题

SELECT
	continent,
NAME 
FROM
	world AS x 
WHERE
	NAME <= ALL ( SELECT NAME FROM world AS y WHERE y.continent = x.continent );

bde60c308809a759ade50f0646620848.png

9题

SELECT NAME,
        continent,
	population 
FROM
	world AS x 
WHERE
	25000000 >= ALL ( SELECT population FROM world AS y WHERE y.continent = x.continent );

29bfaf38bd77b6bf49f3a1ca43d84aa0.png

10题

错误做法:

SELECT NAME
	,
	continent 
FROM
	world AS x 
WHERE
	population >= 3 * ALL ( SELECT population FROM world AS y WHERE x.continent = y.continent );

c7c8b3a43cc5d477087fcff3cbb56db3.png

正确做法:

SELECT NAME,
	continent 
FROM
	world AS x 
WHERE
	population > ALL ( SELECT 3 * population FROM world AS y WHERE y.continent = x.continent AND x.NAME <> y.NAME );

438f4d24d4f996856459215d9e540dde.png

第5关——多表查询

多表查询

更正下面链接中做错过的题

The JOIN operation/zh​sqlzoo.net

参考答案链接地址:

第5关《从零学会SQL:多表查询》练习题答案​mp.weixin.qq.com
3f1bf87b14453c75966dbb4f07db841f.png

8题

/*
问题分析:
1)查找进球球员姓名,对战双方

2)条件:有一个球队是德国(球队编号'GER'),被射入了球
*/


SELECT DISTINCT b.player 
FROM
	game 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去掉重复值
*/

6f8e499e4217c0a34ede90d3ee56929a.png

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;

8d49471ae9186dd90d1ac79b4def9dbb.png

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;

47c2bc93b0835b1007893e90efb9a936.png

错误原因:

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;

d0b8141e2f117c4c02b62287e1cacd83.png

正确解法二:

利用子查询与多表连接,将需要用的字段从少到多一一串起来

SELECT x.`賽事編號`, y.mdate, x.`入球數字`
FROM 
( SELECT
	a.id as `賽事編號`,
	count( b.player ) as `入球數字`
FROM
	game AS a
	INNER JOIN goal AS b ON a.id = b.matchid
WHERE
	( team1 = 'POL' OR team2 = 'POL' ) 
GROUP BY
	a.id ) as x INNER JOIN game AS y ON x.`賽事編號` = y.id;

6351344c32d6f9da60bc26986bc9a608.png

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;

effb6c854bddab000df33de852003e66.png

13题

#  只有当某些字段单独分组时得到的结果一样时,才能联合分组
#  联合分组的好处是使select子句的字段不用聚合函数,以此回避不能对字段使用聚合函数的情况

SELECT
	a.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 
FROM
	game AS a
	LEFT JOIN goal AS b ON a.id = b.matchid 
GROUP BY
	a.id,
	a.mdate,
	a.team1,
	a.team2 
ORDER BY
	a.mdate,
	a.id,
	a.team1,
	a.team2;

ff8421eafb1642583ec8591530127f3e.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值