数据库查询之条件查询

条件

使用where子句对表中的数据筛选,结果为true的行会出现在结果集中

语法如下:

select * from 表名 where 条件;
例:
select * from students where id=1;

where后面支持多种运算符,进行条件的处理
比较运算符
逻辑运算符
模糊查询
范围查询
空判断

比较运算符

等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>

例1:查询编号大于3的学生

select * from students where id > 3;
例:查询编号不大于4的学生

select * from students where id <= 4;

逻辑运算符

and
or
not

例:查询编号大于3的女同学

select * from students where id > 3 and gender=0;

模糊查询

like
%表示任意多个任意字符
_表示一个任意字符

例:查询姓黄的学生

select * from students where name like '黄%';

范围查询

in表示在一个非连续的范围内
空判断
注意:null与’'是不同的
判空is null

例:查询没有填写身高的学生

select * from students where height is null;
判非空is not null

优先级
优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用

猜你喜欢

转载自blog.csdn.net/weixin_44786231/article/details/89492940