concat, replace, mid, xor, round, left, right, <>sql
地址:https://sqlzoo.net/wiki/SELEC...express
表结构
world (name, continent)
name:国家名称
continent:洲api
Q & A
13.找出全部首都和其国家名字,而首都要有国家名字出现。函数
SELECT capital,name FROM world WHERE capital LIKE concat('%',name,'%')
concat
的语法不熟悉。CONCAT函数能够将两个字符串链接为一个字符串,那么对于2个以上字符呢?15."Monaco-Ville"是合并的国家名字,由 "Monaco" 和延伸詞"-Ville"拼接而来.显示国家名字及其延伸词,如首都是国家名字的延伸。 可使用SQL函数REPLACE
或MID
.ui
使用
MID
SELECT name,MID(capital,length(name)+1) as extend FROM world WHERE capital LIKE concat(name,'_%')
使用
REPLACE
SELECT name, REPLACE(capital,name,'') FROM world WHERE capital LIKE concat(name,'_%')
语法:MID(column_name,start[,length])
spa
参数 | 描述 |
---|---|
column_name | 必需。要提取字符的字段。 |
start | 必需。规定开始位置(起始值是 1)。 |
length | 可选。要返回的字符数。若是省略,则 MID() 函数返回剩余文本。 |
语法:REPLACE(original,search,replace)
.net
参数 | 描述 |
---|---|
original | 被搜索的字符串。 |
search | 要搜索并被 replace 替换的字符串。若是 search 是空字符串,则按原样返回原始字符串。 |
replace | 该字符串用于替换 search。若是 replace 是空字符串,则删除出现的全部 search。 |
地址:https://sqlzoo.net/wiki/SELEC...code
表结构
world (name, continent, area, population, gdp) 排序
Q & A
8.Exclusive OR (XOR). Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both. Show name, population and area.教程
SELECT name,population,area FROM world WHERE area>3000000 XOR population>250000000
XOR
逻辑异或。就是两个不能同时成立,也不能同时不成立,只成立其中一个条件.9.Show the name
and population
in millions and the GDP in billions for the countries of the continent
'South America'. Use the ROUND
function to show the values to two decimal places.
For South America show population in millions and GDP in billions both to 2 decimal places.
SELECT name,round(population/1000000,2),ROUND(gdp/1000000000,2) FROM world WHERE continent ='South America'
ROUND
函数用于把数值字段舍入为指定的小数位数. 语法:ROUND(column_name,decimals)
参数 | 描述 |
---|---|
column_name | 必需。要舍入的字段。 |
decimals | 必需。规定要返回的小数位数。 |
12.The capital of Sweden is Stockholm. Both words start with the letter 'S'.
Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
• You can use the function LEFT
to isolate the first character.
• You can use <>
as the NOT EQUALS
operator.
SELECT name,capital FROM world WHERE LEFT(name,1) = LEFT(capital,1) AND name<>capital
知识点:
LEFT(str,len)
RIGHT(str,len)
<>
: 不等于, 比较运算符, 其功能与!=
相同但效率上<>
高。13.Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count because they have more than one word in the name.
Find the country that has all the vowels and no spaces in its name.
• You can use the phrase name NOT LIKE '%a%'
to exclude characters from your results.
• The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'.
SELECT name FROM world WHERE name LIKE '%a%' AND name LIKE '%e%' AND name LIKE '%i%' AND name LIKE '%o%' AND name LIKE '%u%' AND name NOT LIKE '% %'
地址:https://sqlzoo.net/wiki/SELEC...
表结构
nobel (yr, subject, winner)
Q & A
14.The expression subject IN ('Chemistry','Physics')
can be used as a value - it will be 0
or 1
.
Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
SELECT winner, subject FROM nobel WHERE yr=1984 ORDER BY subject IN ('Physics','Chemistry'),subject ,winner
subject in(xxx)为0的分红一组 排序 subject in(xxx)为1的分红一组 排序 获得结果链接起来就是新的排序表.