SQL查询语句大全(个人总结)

SQL查询语句大全(个人总结)全面的sql查询总结

大家好,又见面了,我是你们的朋友全栈君。

前言

在一级项目组时,监控服务器的同时,总结了一下SQL关于查询的语句,希望能给大家带来一些帮助
推荐两个博客,下面借鉴了这两个
菜鸟教程
网上大佬的

之前的总结

在这里插入图片描述
这次在之前的基础上扩展一些内容,分别在字句的后面扩展更多的表达式或者函数。
在这里插入图片描述
下面会用到一级中具体某个表,会有说明

Select

Select+聚合函数

总数

count(*)表示计算总行数,括号中写星与列名,结果相同
例1.查询登录系统学总数

select count(*) from StudentBindPaperTypeEntity
最大值

max(列)求此列的最大值
例2.求下表的最大编号

select max(StudentID) from StudentBindPaperTypeEntity
最小值

min(列)求此列的最小值
例3.求下表编号最小编号

select min(StudentID) from StudentBindPaperTypeEntity
求和

sum(列)求此列之和(注:sum运算符与数字类型连用)
例4.查询当前在线的学生(IsUse=0表示未在线,1表示在线)

select SUM(IsUse) from StudentBindPaperTypeEntity
平均值

avg(列) 表示求此列的平均值(注:avg运算符与数字类型连用)
例5:查询学生编号的平均数

select avg(StudentID) from StudentBindPaperTypeEntity

Select+case…when…then语句

case…when…then语句,相当于编程语言中if判断
例1根据IsUser字段查询学生是否在线

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(表)+连接查询

连接查询

  • 内连接:Inner join
  • 左连接:Left join
  • 右连接:Right join

例子中涉及的表

  • StudentInfoEntity:全校学生的信息
  • ScoreEntity:学生考试的成绩(并不全包含全校学生)

from+inner join

在这里插入图片描述
例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.查询学号>18832650890的学生

select * from StudentBindPaperTypeEntity where StudentID>18832650890

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

select * from StudentBindPaperTypeEntity where StudentID!=18832650890

模糊查询

like
%表示任意多个字符
例1.查询1月8号考试的学生

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

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

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

范围查询

in关键字为非连续查询
例1.查询两个不相邻的学号的学生

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

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

select * from StudentBindPaperTypeEntity where StudentID Between 19100142001 and 19100142006

空判断

is null判断为空
例1.查询没有试卷的学生

select * from StudentBindPaperTypeEntity where PaperType is null

is not null 判断非空
例2.查询有试卷的学生

select * from StudentBindPaperTypeEntity where PaperType is not null

优先级

优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用

group by(分组)

作用:将字段间一对多的关系,向一的方向靠拢分组
例1.查出参加考试有几个学院

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

having的作用跟where子句功能一样,只不过having只用在group by
例3.查出学院ID大于10的学院

select CollegeID  from StudentBindPaperTypeEntity group by CollegeID having CollegeID>10

显示结果:
在这里插入图片描述

Order by(排序)

排序查询语法:

select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
  • 如果列1的值相同,则按照列2排序,以此类推
  • asc从小到大
  • desc从大到小

例1.根据学院分组ID降序(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/131539.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • executescalar mysql_DbCommand.ExecuteScalar 方法的返回值[通俗易懂]

    executescalar mysql_DbCommand.ExecuteScalar 方法的返回值[通俗易懂]DbCommand.ExecuteScalar方法执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。语法:publicabstractObjectExecuteScalar()返回值:类型:System.Object,结果集中第一行的第一列。备注:使用ExecuteScalar方法从数据库中检索单个值(例如一个聚合值)。与使用ExecuteRe…

    2022年6月29日
  • Logback日志工具使用详解

    Logback日志工具使用详解由于Logback比log4j和SLF4J拥有众多优点,如性能(据说有时达到10倍以上),并且支持自动加载配置文件,自动删除旧的日志文件,以及同一个logback配置文件同时适应开发,测试,生产等。因此Logback官方强烈建议开发人员从log4j转到使用Logback。

    2022年6月17日
  • 【python】详解pandas库的pd.merge函数「建议收藏」

    【python】详解pandas库的pd.merge函数「建议收藏」pandas.DataFrame.mergepd.merge(left,right,how=’inner’,on=None,left_on=None,right_on=None,left_index=False,right_index=False,sort=True,suffixes=(‘_x’,’_y’),copy=True,indi…

    2022年4月28日
  • GT注册大全-终结动态修订版「建议收藏」

    GT注册大全-终结动态修订版「建议收藏」本次‘GT注册大全-终结动态修订版’的特点:1.仅中文名称共享软件范围来说为国内乃至世界范围最新最全,通用注册信息有效率最高;2.较之前版本删除了N多已完全失效的注册信息,增加了N多新旧有效注册信息;3.保持了GT注册大全原有的按软件发布时间排列,软件按同系列或同一制作公司及个人软件作者来合并,提供软件相关下载页面或直接下载链接,非通用注册信息一律标明,必要时附带说明等特点;4.进一步完善了‘GT

    2022年5月20日
  • pandas drop参数_pandas concat函数

    pandas drop参数_pandas concat函数pandas中dropna()参数详解DataFrame.dropna(axis=0,how=‘any’,thresh=None,subset=None,inplace=False)1.axis参数确定是否删除包含缺失值的行或列axis=0或axis=’index’删除含有缺失值的行,axis=1或axis=’columns’删除含有缺失值的列,importpandasaspdimportnumpyasnpdf=pd.DataFrame({“name”:[‘Alfr

    2022年9月18日
  • pycharm永久激活码【2021最新】

    (pycharm永久激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~23LNPMIJZT-eyJsaWNlbnNlSWQiOi…

    2022年3月29日

发表回复

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

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