数据库知识整理

本文详细介绍了SQL查询中的where子句、like匹配符、聚合函数、分组汇总、having子句、嵌套查询以及CASE WHEN的使用,通过实例解析了各种查询技巧,帮助读者深入理解数据库查询操作。

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

 

where条件查询知识汇总

语法:

where 条件表达式

常用的运算符

字符匹配符

在where子句中使用匹配符like或not like可以把表达式与字符串进行比较,从而实现对字符串的模糊查询。其语法格式如下:

Where 表达式 [not] like ‘字符串’ [escape ‘换码字符’]

注意,比较字符串是不区分大小写的,如M%和m%是相同的比较运算符。

例子:

Select * from student where name like ‘李_’

Select * from student where name like ‘李%’

Select * from student where name like ‘[李王刘]%’

Select * from student where name like ‘[^李王刘]%’

分类汇总

常用聚合函数的具体用法即含义

例子:

Select count(*) as total from student;

Select count(distinct stuno) from student;

Select max(成绩) as 最高分,min(成绩) as 最低分,avg(成绩) as平均分,sum(成绩) as 总分 from student where stuno=’10010’;

分组汇总

使用聚合函数只返回单个汇总,而在实际应用中,更多的是需要进行分组汇总,如汇总每个同学的平均分、总分等。使用group by字句可以进行分组汇总,为结果集中的每一行产生当一个汇总值。

注意:select 子句中的列表只能包含在group by指定的列或聚合函数指定的列中,例如执行如下命令:

Select 学号,课程号 from student group by 课程号

必须删掉学号改成:

Select 学号,课程号 from student group by 学号,课程号

Select子句包含聚合函数则分类计算,又如:

Select count(*) as num, class from student group by class;

Select 子句包含where或all

如果查询命令在使用where子句的同时又进行分组,那么语句的执行顺序是先确定选出满足条件的记录集,然后对此记录进行分组。又如:

Select stuno,avg(score) as avrscore from student where score > 80 group by stuno;

使用all时,则忽略where子句指定的条件,查询结果包含不满足where子句的分组,相应属性值用空值填充。又如:

Select stuno,avg(score) as avrscore from student where score > 80 group by all stuno;

分组刷选

如果使用group by子句分组,则还可以用having子句对分组后的结果进行过滤筛选。与where子句区别

  1. 作用对象不同:where子句作用于表和视图中的行,而having子句作用于形成的组
  2. 执行顺序不同:若查询语句中同时又where子句和having子句,执行时,先去掉不满足where条件的行,然后分组,分组后再去掉不满足having条件的组。
  3. Where子句中不能使用聚合函数,但having子句的条件中可以包含聚合函数。

例如查询选修4门及以上课程其成绩都在70以上的学生的学号和平均成绩:

Select stuno,avg(score) as avrscore from student where score > 80 group by stuno; having count(lesson)>= 4

嵌套查询

嵌套查询,指一个select查询语句中包含另一个(或多个)select查询语句。例如:

Select stuno,lesson from student where stuno = (select stuno from shool where name = ‘Tom’);

此外还有all any关键字,又如:

Select * from student where birthdate > all(select birthdate forom student where class = ‘1001’) 注出生日期都要比1001班的学生要晚。

Select * from student where birthdate < any(select birthdate forom student where class = ‘1001’) 注出生日期都要比1001班的学生要早。

多列多值嵌套查询

如果子查询中的返回结果是一个多行多列的表,则这样的查询称为多列多值子查询。由于子查询返回多列多值,因此,在主查询中只能使用关键字exists或not exists进行匹配刷选。

又如:

Select * from student where exists (slect * from course where student.lessonno = course.lessonno and lesson=’dadabase technolege’)

 

补充知识case when的使用

case when也是一个很好的分类查询,与我们熟悉编程语言的switch case类似,用于分类执行一些设定的条件查询。

select

case when score > 90 then 'perfect'

when score >80 and score < 90 then 'good'

when score > 60 and score < 80 then 'ok'

else 'bad' end score_class,

count(*)

from student

group by

case when score > 90 then 'perfect'

when score >80 and score < 90 then 'good'

when score > 60 and score < 80 then 'ok'

else null end;

参考:

https://www.cnblogs.com/shuibi/p/6560237.html

https://www.jianshu.com/p/e9355a81c94b

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值