mysql----SELECT names/zh

 
Language: English  • 中文
namecontinent
Afghanistan Asia
Albania Europe
Algeria Africa
Andorra Europe
Angola Africa
....

name:国家名称
continent:洲份

Pattern Matching Strings

此教程使用LIKE运算子来检查国家名字,我们会在world表格中运用SELECT语句:

Summary
 

1.

你可以用WHERE name LIKE 'B%'来找出以B为开首的国家。
%是万用字元,可以用代表任何字完。

找出以Y 为开首的国家。

select name from world where name like 'Y%';

 


2.

找出以Y 为结尾的国家。

select name from world where name like '%Y';

 

 

 

 

3.

“Luxembourg 卢森堡”中有一个x字母,还有一个国家的名字中有x。列出这两个国家。

找出所有国家,其名字包括字母x。

select name from world where name like '%x%';

 

 

 

4.

“Iceland 冰岛”和“Switzerland 瑞士”的名字都是以”land”作结束的。还有其他吗?

找出所有国家,其名字以land 作结尾。

select name from world where name like '%land'

 

 

 

 


5.

“Columbia 哥伦比亚”是以C 作开始,ia 作结尾的。还有两个国家相同。

找出所有国家,其名字以C 作开始,ia 作结尾。

select name from world where name like 'C%'
and name like '%ia';

 

 

 

 

 

 


6.

“Greece 希腊”中有双e 字。哪个国家有双o 字呢?

找出所有国家,其名字包括字母oo。

select name from world where name like '%oo%';

 


7.

“Bahamas 巴哈马”中有三个a,还有吗?

找出所有国家,其名字包括三个或以上的a。

SELECT  name  FROM  world 
  WHERE  name  LIKE  '%a%a%a%'

 

 

 

 

 


8.

“India印度”和”Angola安哥拉”的第二个字母都是n。
你可以用底线符_当作单一个字母的万用字元。

SELECT  name  FROM  world 
 WHERE  name  LIKE  '_n%' 
ORDER  BY  name

找出所有国家,其名字以t作第二个字母。

SELECT name FROM world
 WHERE name LIKE '_t%'

 

 

 

 


9.

“Lesotho 赖索托”和”Moldova 摩尔多瓦”都有两个字母o,被另外两个字母相隔着。

找出所有国家,其名字都有两个字母o,被另外两个字母相隔着。

SELECT name FROM world
 WHERE name LIKE '%o__o%'

 

 

 

 

 

10.

“Cuba古巴”和”Togo 多哥”都是4 个字母。

找出所有国家,其名字都是4 个字母的。

SELECT name FROM world
 WHERE LENGTH(name)=4

 

 

 

 

 

更困难的问题

如你觉得以上题目太容易了,非常好。Well done for getting this far. 下面的题目更困难,更有挑战性!

 
11.

“Luxembourg 卢森堡”的首都capital 都同样叫“Luxembourg”。

显示所有国家名字,其首都和国家名字是相同的。

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

 

 


12.

“Mexico 墨西哥”的首都是”Mexico City”。

显示所有国家名字,其首都是国家名字加上”City”。

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

 

 
 
 

13.

找出所有首都和其国家名字,而首都要有国家名字中出现。

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

 

 
 
 
 
14.

找出所有首都和其国家名字,而首都是国家名字的延伸。
你应显示Mexico City,因它比其国家名字Mexico长。
你不应显示Luxembourg,因它的首都和国家名相是相同的。

SELECT name,capital FROM world
 WHERE capital like concat('%',name,'%') and LENGTH(capital)<>LENGTH(name);

 

 

 

 

 

15.

"Monaco-Ville"是合并国家名字"Monaco" 和延伸词"-Ville".

显示国家名字,及​​其延伸词,如首都是国家名字的延伸。

你可以使用SQL函数REPLACE或MID.

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

 
 
 
原文地址:https://www.cnblogs.com/tk55/p/6750824.html