数据库如何进行有条件的查询?

如题所述

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

语法如下:

select * from 表名 where 条件;
例:
select * from students where id=1;
1
2
3
where后面支持多种运算符,进行条件的处理
比较运算符
逻辑运算符
模糊查询
范围查询
空判断

比较运算符
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>

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

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

select * from students where id <= 4;
1
2
3
4
5
6
逻辑运算符
and
or
not

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

select * from students where id > 3 and gender=0;
1
2
3
模糊查询
like
%表示任意多个任意字符
_表示一个任意字符

例:查询姓黄的学生

select * from students where name like '黄%';
1
2
3
范围查询
in表示在一个非连续的范围内
空判断
注意:null与’'是不同的
判空is null

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

select * from students where height is null;
判非空is not null
1
2
3
4
优先级
优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用
————————————————
温馨提示:答案为网友推荐,仅供参考
相似回答