where条件查询语句

1. where条件查询介绍

使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。

where语句支持的运算符:

  1. 比较运算符
  2. 逻辑运算符
  3. 模糊查询
  4. 范围查询
  5. 空判断

where条件查询语法格式如下:

select * from 表名 where 条件;

例:
select * from students where id = 1;

2.比较运算符查询

  1. 等于:=
  2. 大于:>
  3. 大于等于:>=
  4. 小于:<
  5. 小于等于:<=
  6. 不等于:!=或<>

举例:

-- 查询编号大于3的学生
select * from students where id > 3;

在这里插入图片描述

-- 查询编号不大于4的学生
select * from students where id <= 4;

在这里插入图片描述

-- 查询姓名不是黄蓉的学生
select * from students where name != '黄蓉';
select * from students where name <> '黄蓉';

在这里插入图片描述

-- 查询没被删除的学生
select * from students where is_del = 0;

3.逻辑运算符查询

  1. and:查询两个条件都成立的结果
  2. or:查询有一个条件成立的结果
  3. not:对查询结果取反显示

举例:

-- 查询编号大于3的女同学
select * from students where id > 3 and gender = '女';

在这里插入图片描述

-- 查询编号小于4或没被删除的学生
select * from students where id < 4 or is_del = 0;

在这里插入图片描述

-- 查询年龄不在10岁到15岁之间的学生
select * from students where not (age >= 10 or age <=15);

在这里插入图片描述

4.模糊查询

  1. like:是模糊查询关键字
  2. %:表示任意多个字符
  3. _:表示任意一个字符

举例

-- 查询姓黄的学生
select * from students where name like '黄%';

在这里插入图片描述

-- 查询姓黄并且名是一个字的学生
select * from  students where name like '黄_';
select * from students where name like '黄__';

在这里插入图片描述

-- 查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';

在这里插入图片描述

5.范围查询

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

举例:

-- 查询编号为3至8的学生
select * from students where id >=3 and id <=8;
select * from students where id between 3 and 8;

在这里插入图片描述

-- 查询编号不是3至8的男生
select * from students where gender='男' and not (id between 3 and 8);	

在这里插入图片描述

-- 查询编号是3、5、7的学生
select * from students where id in (3,5,7);

在这里插入图片描述

-- 查询编号不是3、5、7的学生
select * from students where id not in (3,5,7);

在这里插入图片描述

6.空判断

  1. 判断为空使用:is null
  2. 判断非空使用:is not null

举例:

-- 添加height字段,设置为两位小数的浮点数
alter table students add height decimal(3,2);
-- 设置height值
update students set height=1.85;
update students set height = null where id in (2, 4, 6);
update students set height=1.75 where id = 9;

-- 查询没有填写身高的学生
select * from studetns where height is null;

在这里插入图片描述
– 查询填写身高的学生
select * from studetns where height is not null;
在这里插入图片描述

7. 小结

  • 常见的比较运算符有>,<,>=,<=,!=
  • 逻辑运算符and表示多个条件同时成立则为真,or表示多个条件有一个成立则为真,not表示对条件取反
  • like和%结合使用表示任意多个字符,like和_结合使用表示任意字符
  • between-and限制连续性范围,in限制非连续性范围
  • 判断为空使用:is null
  • 判断非空使用:is not null
  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值