SQL查询语句大全(大佬总结,必看)

SQL查询语句大全(大佬总结,必看)前言在一级项目组时 监控服务器的同时 总结了一下 SQL 关于查询的语句 希望能给大家带来一些帮助推荐两个博客 下面借鉴了这两个菜鸟教程网上大佬的之前的总结这次在之前的基础上扩展一些内容 分别在字句的后面扩展更多的表达式或者函数 下面会用到一级中具体某个表 会有说明 SelectSelect 聚合函数总数 count 表示计算总行数 括号中写星与列名 结果相同例 1 查询登录系统学总数 selectcount fromStudentB

目录

前言

之前的总结

SelectSelect+聚合函数

总数

最大值

最小值

求和

平均值

Select+case…when…then语句

​select+top

from(表)+连接查询

from+inner join

from+left join

from+right join

Where(条件语句查询)

比较运算符

模糊查询

范围查询

空判断

优先级

group by(分组)

​group by+聚合函数

group by+having

Order by(排序)


前言

之前的总结

SQL查询语句大全(大佬总结,必看)

这次在之前的基础上扩展一些内容,分别在字句的后面扩展更多的表达式或者函数。

SQL查询语句大全(大佬总结,必看)

下面会用到一级中具体某个表,会有说明

Select
Select+聚合函数

总数

select count(*) from StudentBindPaperTypeEntity 

最大值

select max(StudentID) from StudentBindPaperTypeEntity

最小值

select min(StudentID) from StudentBindPaperTypeEntity

求和

select SUM(IsUse) from StudentBindPaperTypeEntity

平均值

select avg(StudentID) from StudentBindPaperTypeEntity 

Select+case…when…then语句

select a.StudentID, (case a.IsUse when '0' then '未在线' when '1' then '在线' else '未上传' end) as 在线情况 from StudentBindPaperTypeEntity as a

显示情况:

在这里插入图片描述
select+top

top:取表中前多少的数据

例1.取出表中第几行数据(如第一行)

select top 1 * from StudentBindPaperTypeEntity 

例2.取出表中百分之多少数据

select top 50 percent * from StudentBindPaperTypeEntity  

from(表)+连接查询

连接查询

from+inner join

SQL查询语句大全(大佬总结,必看)

例1.查出这两个表中共有的信息(as为表的别名,方便)

select score.studentID,score.score,s.CollegeID,s.major,s.majorClass from ScoreEntity as score inner join StudentInfoEntity as s on score.studentID=s.studentID  where score.CollegeID=02

显示结果

在这里插入图片描述

from+left join

 在这里插入图片描述

左外连接:左表的值会全部显示出来,右表的值显示on条件搜索的的结果,搜索不到为NULL
例1两个表作左外连接

select score.studentID,score.score,s.CollegeID,s.major,s.majorClass from  StudentInfoEntity as s left join ScoreEntity as score on s.studentID=score.studentID 

显示结果:(个别)

在这里插入图片描述

from+right join

 在这里插入图片描述

右外连接与左外连接相反(右表的值全部显示出来)
例1两个表做右外连接

select score.studentID,score.score,s.CollegeID,s.major,s.majorClass from  ScoreEntity as score  right join StudentInfoEntity as s on s.studentID=score.studentID

现在两个表换了位置,结果是一样的

在这里插入图片描述

Where(条件语句查询)

 在这里插入图片描述

比较运算符

例1.查询学号>的学生

select * from StudentBindPaperTypeEntity where StudentID> 

例2.查询学号!=的学生(<>同效)

select * from StudentBindPaperTypeEntity where StudentID!= 

模糊查询

select * from StudentBindPaperTypeEntity where TimeTamp like '2020-01-08%' 

例2.查询不是1月8号考试的学生

select * from StudentBindPaperTypeEntity where TimeTamp not like '2020-01-08%' 

范围查询

select * from StudentBindPaperTypeEntity where StudentID in('','') 

Between…and…为连续查询(注:sql软件情况不一样,可能不包含and后的值)
例2.查询两个学号之间的学生

select * from StudentBindPaperTypeEntity where StudentID Between  and  

空判断

select * from StudentBindPaperTypeEntity where PaperType is null 
select * from StudentBindPaperTypeEntity where PaperType is not null 

优先级

group by(分组)

select CollegeID  from StudentBindPaperTypeEntity group by CollegeID 

显示结果:

在这里插入图片描述
group by+聚合函数

例2.查出各个学院参加考试的人数

select CollegeID, count(StudentID) from StudentBindPaperTypeEntity group by CollegeID  

显示结果:

在这里插入图片描述

其实group by + 聚合函数是从group by + group_concat()演变过来的,SqlServer不支持这个函数

group by+having

select CollegeID  from StudentBindPaperTypeEntity group by CollegeID having CollegeID>10 

显示结果:

在这里插入图片描述

Order by(排序)

排序查询语法:

 select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...] 
select CollegeID  from StudentBindPaperTypeEntity group by CollegeID order by CollegeID desc 

例2.将上表升序(asc)

select CollegeID  from StudentBindPaperTypeEntity group by CollegeID order by CollegeID asc 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211463.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:22
下一篇 2026年3月18日 下午10:22


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号