使用select对数据表进行查询时,可以添加where+条件对数据进行筛选。
一、 比较运算符
运算符 | 功能 | 示例 |
---|---|---|
> | 大于 | select * from student where age > 14; |
< | 小于 | select * from student where age < 18; |
>= | 大于等于 | select * from student where age >= 15; |
<= | 小于等于 | select * from student where age <= 16; |
!=/<> | 不等于 | select * from student where age != 14; |
二、 逻辑运算符
运算符 | 功能 | 示例 |
---|---|---|
and | 与,多个条件同时成立 | select * from student where age > 14 and age < 18; |
or | 或,多个条件任意一个成立 | select * from student where age > 14 and gender = ‘男’; |
not | 非,取反 | select * from student where not age > 14; |
三、 模糊查询
模糊查询有两种方法,like或者rlike。
——like:用%代替一个或多个字符;用_代替一个字符。
示例1:查询名字中以“李”开头的学生:
select * from student where name like "李%";
示例2:查询名字中含有“林”的学生:
select * from student where name like "%林%";
示例3:查询有三个字的名字的学生:
select * from student where name like "___";
示例4:查询有三个字以上的名字的学生:
select * from student where name like "___%";
——rlike:以正则表达式对字符进行匹配。
示例1:查询名字中以“李”开头,以“宁”结尾的学生:
select * from student where name like "^李.*宁$";
四、 范围查询
——in:非连续的范围查询。
示例1:选择年龄是14、16、18岁的学生:
select * from student where name in (14,16,18);
——between …… and ……:表示连续范围内的信息。
示例1:查询年龄在12到18岁的学生:
select * from student where name between 12 and 18;
示例2:查询年龄不在12到18岁的学生:
select * from student where name age not between 12 and 18;
五、 判断是否为空
——is null / is not null
示例1:查询数据表中姓名为空的记录:
select * from student where name is null;
示例2:查询数据表中年龄不为空的记录:
select * from student where age is not null;