【03】DQL语言——条件查询

本文介绍了SQL查询的基础知识,包括使用SELECT从表中提取数据,运用条件运算符(如>,<,=)进行筛选,利用AND,OR,NOT进行逻辑组合,以及LIKE,BETWEEN,IN等进行模糊匹配和范围查询。同时,提到了如何处理NULL值的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语法:
select 查询列表 from 表名;
where 筛选条件;

筛选条件分类:

1、 按条件运算符分类:> ; < ; = ; ! ; = ; <> ; >= ; <=

# 查数学成绩大于90的学生信息
select * from students 
where Math_score>90;

# 查询小组编号不等于5的学生姓名和小组编号
select name,group_id 
from students 
where group_id!=5;

# 查询学生表中,小组编码group_id不为‘1’的学生信息
select * from students 
where group_id<>'1';

2、 按逻辑表达式分类: && ; || ; ! ; and ; or ; not
作用:用于连接条件表达式
&&和and:若俩个条件都为turn,结果就为true,反之为false
||和or:只要有一个条件为true,结果就为true,反之为false
!和not:如果连接的条件本身为false,结果则为true,反之为false

# 查询总成绩在300到400之间的学生姓名和政治、英语、数学、专业课成绩
select name,Politics_score,English_score,Math_score,Major_score
from students
where sum_score>=300 and sum_score<=400;

# 查询总成绩在300到400之间或者高于450的学生姓名和政治、英语、数学、专业课成绩
select name,Politics_score,English_score,Math_score,Major_score
from students
where sum_score>=300 and sum_score<=400 or sum_score>=450;

3、 模糊查询:like ; between and ; in ; is null
特点:一般和通配符(%、_)搭配使用,
若通配符本身为查找对象,需要在其前面加’'转义符
或在语句后面使用(ESCAPE ‘转义符’)指定转义符

# 查询学生名中包含字符a的学生信息
select * from students
where last_name like '%a%'; # %为通配符,表示任意多个字符,包含0个,但不包含null

# 查询学生名中第三个字符为a,第五个字符为g的学生名和总成绩
select last_name,sum_score from students
where last_name like '__a_g%'; # _为通配符

# 查询学号在10到20之间的学生信息
select * from students
where stu_id>=10 and stu_id<=20;
# ----------等价于--------------
select * from students
where stu_id between 10 and 20; # 包含临界值,但大小顺序不能换

# 查询学生的分组编号是1、3、6的学生信息
select * from students
where group_id='1' or group_id='3' or group_id='6';
# ----------等价于--------------
select * from students
where group_id in('1', '3', '6'); # 判断字段内值是否属于in列表中的值

# 查询没有获得奖学金的学生信息
select * from student
where scholarship is null; # is是为了解决=不能判断null的情况
# 查询获得奖学金的学生信息
select * from student
where scholarship is not null;

4、安全等于 <=> :
可以解决=不能判断null,和is不能判断数值型数据的情况


# 查询没有获得奖学金的学生信息
select * from student
where scholarship <=> null;
# 查询总成绩为350的学生信息
select * from students
where sum_scor<=>350;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有品位的小丑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值