您的位置:首页 > 数据库

SQLzoo刷题-%、_、concat、replace

2020-08-01 18:11 20 查看

1、通配符%

%可以代表0个、1个、多个字符

  • 以a开头-----“a%”
  • 以a结尾-----“%a”
  • 含有a----"%a%"
/*筛选出name中含有aeiou所有元素的,但不含空格的*/
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 '% %'`

2、通配符_

_仅代表一个字符

  • 找出名字是4个字母的:where name like “____”

3、concat函数

  • concat是用来连接两个字符串的函数
/*①筛选所有国家名字,其首都是国家名字加上”City”*/
select name
from world
where capital = concat(name,' City')
/*②筛选出首都、国家,其首都包含国家名*/
select capital,name
from world
where capital like concat('%',name,'%')

注意:concat将两个字符连接起来是没有空格的,如concat(“hello”,“cat”) 得到的结果是hellocat,若想得到hello cat,则需自己添加空格,concat(“hello”," cat")

4、replace函数

  • replace( ‘st1’ , ‘str2’ , ‘str3’ )
    st1: 被搜索的字符串。
    str2: 在st1中要被 str3 替换的字符串。
    str3: 该字符串用于替换 str2。如果str3是空字符串,那么直接在st1中删除str2。
    如,replace(‘hellocat’,‘hello’,‘goodbye’)表示用goodbye替换hellocat中的hello
/*筛选国家名字,及其扩展词,首都是国家名字的延伸。*/
select name, replace (capital, name, '') as extend /*由于’‘为空字符串,因此在capital中删除name*/
from world
where capital like concat ( name, '%_' ) ;/*扩展词至少包含一个字符,故使用 '%_' */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: