![2641c7d23866738516991eef431f6180.png](https://img-blog.csdnimg.cn/img_convert/2641c7d23866738516991eef431f6180.png)
这次通过navicat和小海豚见面,我的“交流”技术进步了,所以换头图了,长大了就是不一样。
理想美好,现实问题往往复杂。但一番化繁为简后,我们总能把大问题拆成小问题,各个击破,取得胜利。(没错这就是逻辑树的分析方法)
每一个小问题的解决方法,就是一个个sql代码块。如果这类问题经常遇到,把解决办法(代码块)存起来,就成了sql视图,以后遇到这类问题,直接调用视图搞定,不用再浪费脑力和体力。有些小问题并不经常遇到,完全可以遇到了再写sql代码块,这就是子查询。
类比买车,如果用车频数高:买辆车,就是创建视图;如果用车频数低:打车,即创建子查询。类比python,sql的视图,更像python中的‘类’:class;sql的子查询,像python中的函数:function。
前面学的基础查询语句,在复杂查询里,可用来“造句”。
1.当前装备
下面是目前本玩家获得的装备,过关前复盘一下,知晓自己是多么的富有。
![2def5e27b0cb80f362a6d0c57a4e7c8e.png](https://img-blog.csdnimg.cn/img_convert/2def5e27b0cb80f362a6d0c57a4e7c8e.png)
2.闯关经验总结
SELECT within SELECT Tutorial部分:
4.Which country has a population that is more than Canada but less than Poland? Show the name and the population.
错误的第一次:我使用了between a and b,结果错误,因为between包含两端的值。
语句:
select name,population
from world
where population between (
select population
from world
where name='canada')
and (
select population
from world
where name='poland');
结果,只显示了两端值,canada和poland:
![1403a127d0db1ce86df4da3f46fde3bb.png](https://img-blog.csdnimg.cn/img_convert/1403a127d0db1ce86df4da3f46fde3bb.png)
正确的第二次,用两个条件,语句正确:
select name,population
from world
where population >(
select population
from world
where name='canada')
and population<(
select population
from world
where name='poland');
打开新世界的第三次:看了猴子老师的解答,发现between..and..也能用。+1-1就可以去掉between的两端值(还挺好玩的,这都能+1-1,sql也是灵活呀)。正确语句如下:
select name,population
from world
where population between
(select population
from world
where name='canada')+1
and
(select population
from world
where name='Poland')-1;
5.Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.
![e5ec8294cc1ef3b22e118038ab7d4a52.png](https://img-blog.csdnimg.cn/img_convert/e5ec8294cc1ef3b22e118038ab7d4a52.png)
此题不会,因为不知道select里面可以写子查询,直接看了答案。总结:1,select里面可以写子查询。2,round函数不需要写2位。3,写population/germany公式的时候,先*100。正确语句:
select name,concat(
round(population*100/(
select population
from world
where name='germany'
)),'%') as percentage
from world
where continent='europe';
6.显示出gdp高于欧洲任何国家的国家名字。(有些国家,gdp是null)
第一次,没注意null,结果不对。
第二次,在where语句中,加了gdp<>null,结果不对,说明数值与null之间无可比性。
第三次,使用gdp is not null,结果正确:
![9b266939d9e0ea023375deb68cc665a8.png](https://img-blog.csdnimg.cn/img_convert/9b266939d9e0ea023375deb68cc665a8.png)
7.找出各大洲area最大的国家。
第一次,使用max函数,结果不对,因为运行顺序:1⃣从wolrd取数,2⃣按大洲分组,3⃣呈现三列:洲名,国家名,area最大值。此处最大值不准确。
select continent,name,max(area) as area
from world
group by continent
第二次,坚持写了group by continent,用了关联子查询:where w1.continent=w2.continent,结果报错。因为1,不用分组,每一次用>all(关联子查询),最终比出的就是每个continent里面最大的了。2,需要用>=all(关联子查询),因为最大值也在里面,需要=,否则结果为空。正确代码:
select continent,name,area
from world as w1
where area>=all(
select area
from world as w2
where w1.continent=w2.continent
);
8.列出每个大洲及其国家的名字,都按照字母表顺序排序。
错误总结:复习了order by语句,但注意,不是动词ordered by。正确语句:
select continent,name
from world
group by continent
order by continent asc,name asc
9.找到所有国家人口都<=25000000的大洲,列出洲名,国家名和人口。
错误1,使用分组:group by continent,运行错误。题意并不需要分组,只要找到这样的国家全部列出来即可。
答案思路:所有国家人口都<=25000000,即25000000>=所有国家人口,sql语句:where 25000000>=all(关联子查询)。正确语句如下:
select name,continent,population
from world as w1
where 25000000>=
all(select population
from world as w2
where w1.continent=w2.continent)
10.找出一个大洲内,一个国家比其他所有邻国的人口数量均高出3倍多的国家名称。
错误总结:在子查询中没有写w1.name<>w2.name,运行结果为空。因为国家名称重复后,无法比较。正确语句:
select name,continent
from world as w1
where population/3>all(
select population
from world as w2
where w1.continent=w2.continent
and w1.name<>w2.name
)