1.
“Mexico 墨西哥” 的首都是”Mexico City”。
顯示所有國家名字,其首都是國家名字加上”City”。
concat 函數:函數 concat 可以用來合拼兩個或以上的字串
SELECT name
FROM world
WHERE capital LIKE concat(name, ' City')
2.
找出所有首都和其國家名字,而首都要有國家名字中出現。
select capital, name
from world
where capital like concat('%',name,'%')
3.
找出所有首都和其國家名字,而首都是國家名字的延伸。
你應顯示 Mexico City, 因它比其國家名字 Mexico 長。
你不應顯示 Luxembourg, 因它的首都和國家名相是相同的。
select name, capital
from world
where capital like concat(name,'%') and capital != name
比名字长,不能一样,得用and并列。
4.
"Monaco-Ville" 是合併國家名字 "Monaco" 和延伸詞 "-Ville".
顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
你可以使用 SQL 函數 REPLACE 或 MID.
select name,replace(capital,name,'') as name_1
from world
where capital like concat(name,'_%')
replace 函数:
REPLACE(f, s1, s2) returns the string f with all occurances of s1 replaced with s2.
例 1:
replace('vessel','e','a') <- 'vassal'
用空字符代替name,然后把这一列设为name1