数字比较 & 字符比较 & 逻辑比较
数值:
=,!=,>,>=,<,<=
字符:
=,!=
逻辑:
and or
范围内比较
判断是否在范围之间:
between value1 and value2
判断是否在范围内:
in(value1,value2,...)
不在范围内:
not in(value1,value2,...)
匹配空和非空
空:
is null
非空:
is not null
示例:查找姓名为空的男英雄
select * from WANGZHE where name is null and sex='男';
查找姓名为 "" 的英雄的 id ,姓名和国家:
select id,name,country from WANGZHE where name="";
备注:
- NULL:空值,必须用
is或者is not去匹配。 -
"":空字符串,只能用=或者!=去匹配。
模糊比较
语法:
where 字段名 like 表达式
其中,常用的模糊比较表达式如下。
匹配单个字符的小划线:
_
匹配 0 到多个字符的百分号:
%
示例:查询姓名包含两个或两个以上字符的记录
>> select * from WANGZHE where name like "_%_";
查询姓名包含零个或多个字符的记录,不含 NULL,NULL 需要用 is[is not] 匹配:
>> select * from WANGZHE where name like "%";
姓名字段由两个字组成的记录:
>> select * from WANGZHE where name like "___";
查询 “赵” 姓的记录:
>> select * from WANGZHE where name like "赵%";
查询表名中包含 sa 字符串的表:
>> show tables like "%sa%";
查询变量名中包含 character 的变量:
>> show variables like "%character%";