- 模糊查询
%:表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
比如 SELECT * FROM [user] WHERE u_name LIKE '%三%'
将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来。
另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '%猫%'
若使用 SELECT * FROM [user] WHERE u_name LIKE '%三%猫%'
虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”
%:0个或1个或多个
_:只能是1个,不多不少
2.concat 拼接
a.“Mexico 墨西哥”的首都是”Mexico City”。
顯示所有國家名字,其首都是國家名字加上”City”
select name from world where capital = concat(name,' City')
b.找出所有首都和其國家名字,而首都要有國家名字中出現。
select capital,name from world where capital like concat('%',name,'%')
c.找出所有首都和其國家名字,而首都是國家名字的延伸。
你應顯示 Mexico City,因它比其國家名字 Mexico 長。
你不應顯示 Luxembourg,因它的首都和國家名相是相同的。
select name,capital from world where capital like concat(name,'_','%')
3.MID函数:从文本字段中提取字符
语法:SELECT MID(column_name,start[,length]) FROM table_name
start:从1开始
4.replace函数:
REPLACE(f, s1, s2)
把字段f中的所有 s1 替换为 s2
把world表 name字段 中的所有 a 替换为 .
select name,replace(name,'a','.') from world
5.replace concat
"Monaco-Ville"是合併國家名字 "Monaco" 和延伸詞"-Ville".
顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
select name,replace(capital,name,"") as x from world where capital like concat(name,"_%")
6.单引号需要转义
两个单引号表示一个单引号
查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節
select *from nobel where winner = 'EUGENE O''NEILL'
7.desc 降序
asc 升序
8.Round()函数
select round(column_name,decimals)from table_name
column_name:必需,要舍入的字段
decimals:必需,规定要返回的小数位数