SQL中的where条件查询——- 比较运算符 - 逻辑运算符 - 模糊查询 - 范围查询 - 空判断

where支持的运算符

  • 比较运算符
  • 逻辑运算符
  • 模糊查询
  • 范围查询
  • 空判断

where条件查询的格式

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

比较运算符

  • 等于 =
  • 大于 >
  • 大于等于 >=
  • 小于 <
  • 小于等于 <=
  • 不等于 != 或 <>
例1:查询编号大于3的学生:

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

select * from students where id <= 4;
例3:查询姓名不是“黄蓉”的学生:

select * from students where name != '黄蓉';
例4:查询没被删除的学生:

select * from students where is_delete=0;

逻辑运算符查询

  • and
  • or
  • not

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

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

在这里插入图片描述
例2:查询编号小于2或没被删除的学生:

select * from students where id < 2 or is_delete=0;

在这里插入图片描述
例3:查询年龄不在23岁到24岁之间的学生:

select * from students where not (age >= 23and age <= 24);

在这里插入图片描述

说明:

多个条件判断想要作为一个整体,可以结合‘()’。

模糊查询

  • like是模糊查询的关键字
  • %表示任意多个字符
  • _表示一个任意字符

例1:查询姓黄的学生:

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

在这里插入图片描述
例2:查询姓黄并且“名”是一个字的学生:

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

在这里插入图片描述
例3:查询姓黄或叫什么琦的学生:

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

在这里插入图片描述

范围查询

  • between…and…表示在一个连续的范围内查询
  • in表示在一个非连续的范围内查询

例1:查询编号为3至8的学生:

select * from students where id between 3 and 8;

或者

select * from students where id>=3 and id<=8;

在这里插入图片描述
例2:查询编号不是3至8的男生:

select * from students where (not id between 3 and 8) and gender='男';

select * from students where not (id>=3 and id<=8);

在这里插入图片描述

空判断

  • 判断为空使用 is null
  • 判断为非空使用 is not null

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

select * from students where height is null;

在这里插入图片描述
注意:

1.不能使用 where height = null 判断为空
2.不能使用 where height != null 判断非空
3. null 不等于 ’ ’ 空字符串

发布了868 篇原创文章 · 获赞 1228 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/qq_35456045/article/details/105158133