- like与通配符
SELECT * FROM gb_members WHERE kitchen_skill LIKE '%鸡'
备注:百分号“%”在与 LIKE 搭配使用时,代表“任意多个任意字符”
SELECT * FROM gb_members WHERE kitchen_skill LIKE '%鸡%'
SELECT * FROM person WHERE email LIKE 'h%taohuadao.com'
SELECT * FROM person WHERE NOT (email LIKE 'h%@taohuadao.com' or email LIKE '%@baituoshan.com')
SELECT * FROM person WHERE description LIKE '%郭靖%' and description LIKE '%洪七公%'
备注:(1)如果查找的条件是只要包含某字符而不论该字符出现的位置,那么在该字符的前后加上“%”;(2)“%”虽然可以表示任意个数的字符,但是不能准确限制字符的个数。
SELECT * FROM person WHERE p_name like '_风'
备注:下划线“_”可以代表任意字符串,但是只代表一个
SELECT * FROM person WHERE description LIKE '%\%'
备注:如果需要查询的字符是“%”或“_”本身,应该使用反斜线“\”对通配符进行转义,将其变为普通字符
-
计算字段的应用
SELECT * FROM football WHERE GS-GA>50
SELECT * FROM football WHERE (GS-GA)/(win+draw+lose)>1.2
备注:(1)用字段计算的结果作为筛选条件,只要参与计算的字段都是数字,就可以使用算数运算对字段进行计算
(2)由于无法利用建立索引等原因,在 WHERE 中使用计算字段可能会降低查询效率
SELECT team,GS-GA FROM football WHERE (GS-GA)/(win+draw+lose)>1.2
SELECT *, GS-GA, win+draw+lose, (GS-GA)/(win+draw+lose) FROM football WHERE (GS-GA)/(win+draw+lose)>1.2
备注:可以将计算字段写在 SELECT 后面,相当于在结果中添加一个原表中没有的“自定义列”
SELECT *, GS-GA as 净胜球总数, win+draw+lose as 场数, (GS-GA)/(win+draw+lose) as 场均净胜球 FROM football WHERE (GS-GA)/(win+draw+lose)>1.2
备注:使用 AS 关键字,对 SELECT 的任何字段(包括自定义列)分别起“别名”,结果更加易读
SELECT *, GS-GA as 净胜球总数, win+draw+lose as 场数, (GS-GA)/(win+draw+lose) as 场均净胜球 FROM football WHERE (GS-GA)/(win+draw+lose)>1.2 ORDER BY 净胜球总数/场数
备注:
(1)不能在 SELECT 或 WHERE 中直接引用别名进行计算;
(2)可以在 ORDER BY 中使用别名进行排序;
(3)由于SQL语句的执行过程是:FROM -> WHERE -> SELECT -> ORDER BY ,所以在 WHERE 或 SELECT 中引用别名时 SELECT 还没有执行,但是在 ORDER BY 中可以使用。