侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL常用SQL语句总结包含复杂SQL查询

数据库  /  管理员 发布于 6年前   287

1、复杂SQL查询

1.1、单表查询

(1)选择指定的列

[例]查询全体学生的学号和姓名

select Sno as 学号,Sname as 姓名 from student;select Sno,Sname from student;

(2)查询全部列

[例]查询全体学生的详细信息

select * from student;

(3)对查询后的指定列进行命名

[例]查询全部学生的“姓名”及其“出生年”两列

select Sname as 姓名,(2014-Sage) as 出生年 from student;select Sname ,(2014-Sage) from student;

(4)消除取值重复的行

[例]查询选修了课程的学生学号

select distinct Sno as 选修了课程的学生学号 from SC;select distinct Sno from SC;

(5)选择表中若干元组(满足条件的)

1.2、大小比较

[例]查询计算机系(IS)全体学生名单

select Sname as 学生姓名 from student where Sdept='IS';

[例]查询全体20岁以下的学生姓名和年龄

select Sname as 姓名,Sage as 年龄 from student where Sage<20;

1.3、确定范围

[例]查询所有在20到23岁(含20和23)的学生姓名、系别和年龄

select Sname as 姓名,Sdept as 系别,Sage as 年龄 from student where Sage between20 and 23;

注意between 小数 and 大数。

1.4、in和not in确定集合

[例]查询IS系和CS系的全体学生姓名和性别

select Sname as 姓名,Ssex as 性别 from student where Sdept='IS' or Sdept='CS';select Sname as 姓名,Ssex as 性别 from student where Sdept in ('IS','CS');

[例]查询既不属于IS系,也不属于MA系的学生姓名和年龄

select Sname as 姓名,Sage as 年龄 from student where Sdept !='IS'and Sdept!='CS';select Sname as 姓名,Sage as 年龄 from student where Sdept not in('IS','MA');

1.5、字符匹配(like % _ )

[例]查询所有姓李的学生姓名和性别

select Sname as 姓名,Ssex as 性别 from student where Sname like '李%';

[例]查询所有“2002”年入学的学生学号、姓名和系别

select Sno as 学号,Sname as 姓名,Sdept as 系别 from student where Sno like'2002%';

[例]查询所有不姓“刘”的学生信息

select * from student where Sname not like'刘%';

[例]查询名称含有“数据”的课程号、课程名及学分

select Cno as 课程号,Cname as 课程名,Ccredit as 学分 from course where Cname like '%数据%';

总结:

select * from course where cname like '%数据%';包含数据的字符串 select * from course where cname like '数据%';以数据开头的字符串select * from course where cname like '%数据'; 以数据结尾的字符串

1.6、涉及空值的查询(is null)

[例]查询没有先修课的课程号和课程名

select Cno as 课程号,Cname as 课程名,Cpno from course where Cpno is null;

[例]查询所有有成绩的学生学号、课程号及成绩

select Sno as 学号,Cno as 课程号,Grade as 成绩 from SC where Grade is not null;

1.7、查询结果排序(order by )

[例]查询选修了3号课程的学生学号和成绩,结果按成绩降序排列。

select Sno as 学号,Grade as 成绩 from SC where Cno=3 order by Grade desc;

[例]查询选修了3号课程的学生学号和成绩,结果按成绩升序排列。

select Sno as 学号,Grade as 成绩 from SC where Cno=3 order by Grade asc;

1.8、聚集函数

count、sum、avg、max、min

[例]查询学生总数

select count(*) as 学生总数 from student;

[例]查询所有课程的总学分

select sum(Ccredit) as 所有课程总学分 from course;

[例]查询全体学生平均年龄

select avg(Sage) as 平均年龄 from student;

[例]查询1号课程的最高分

select max(Grade) as 1号课程的最高分 from SC where Cno=1;

1.9、分组统计(group by)

[例]查询男女学生各有多少人。

select Ssex as 性别,count(*) as 人数 from student group by Ssex;

[例]查询每个课程的课程号和平均分。

select Cno as 课程号,avg(Grade) as 平均分 from SC group by Cno;

【例】查询选修了3门课程以上(含3门)的学生学号和选修课程数。

select Sno as 学号 ,count(course.Cno) as 选修课程数From SC,courseWhere course.Cno=SC.CnoGroup by SnoHaving Count(course.Cno)>=3;

having 关键字后面直接跟聚集函数

在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。

SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value

【例】查询选修了2门课程以上(含2门,但不含1号课程),学生学号和选修课程数。

select Sno as 学号 ,count(course.Cno) as 选修课程数From SC,courseWhere course.Cno=SC.Cno and course.Cno !=1Group by SnoHaving Count(course.Cno)>=2;

【例】查询不及格门数2门以上的学生学号。

Select Snofrom scWhere sc.Grade<60Group by SnoHaving count(Cno)>=2;

【例】查询有2名以上(含2名)学生选修了的课程号和选修人数。

Select Cno,count(Sno)From SCGroup by CnoHaving count(sno)>=2

2、连接查询

(1)等值与非等值连接查询

[例]查询每个学生及其的选修课程情况

select student.Sno as 学号,course.Cno as 选修课号,SC.Grade as 成绩 from student,course,SC where student.Sno=SC.Sno and course.Cno=SC.Cno ;

(2)自身连接

[例]查询每个学生的间接选修课

select SC.Sno as 学号,FIRST.Cname as 直接选修课,SECOND.Cname as 间接选修课from SC,course as FIRST,course as SECONDwhere FIRST.Cno=SC.Cnoand FIRST.Cpno=SECOND.Cno;

(3)外连接

[例]查询所有学生选修课程情况(含没选修课程的学生)

select student.Sno as 学号,Sname as 姓名,sc.Cno as 选修课程号from student LEFT OUTER JOIN SC ON student.Sno=SC.Sno;

join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据

JOIN: 如果表中有至少一个匹配,则返回行LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行FULL JOIN: 只要其中一个表中存在匹配,就返回行
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

3 、嵌套查询

(1)带有IN谓词的子查询( 属性 in (子查询的查询结果) )

【例】查询与王敏同学在同一个系的学生信息。

select *from studentwhere Sdept in ( select Sdept from student where Sname='王敏');

【例】查询不与王敏同学不在同一个系的学生信息。

select *from studentwhere Sdept not in ( select Sdept from student whereSname='王敏');

【例】查询选修了课程名是“信息系统”的学生学号和姓名。

select student.Sno as 学号, Sname as 姓名from student,SCwhere student.Sno=SC.Sno and Cno in ( select Cno from course where Cname='信息系统')

【例】查询曾与刘晨一同上课的学生学号和姓名。(假设:一个课程只有一个上课班)

select distinct student.Sno as 学号, Sname as 姓名from student,SCwhere student.Sno=SC.Sno and Cno in ( select Cno from SC,student where SC.Sno=student.Sno and student.Sno in ( select Sno from student where student.Sname='刘晨' ))
  • 内层in 查出刘晨的学号sno,外层in查出刘晨所上课程的课程号。

(2)带有比较运算符的子查询(=,>=,<=,<>或!=)

【例】查询与王敏同学在同一个系的所有学生信息  (=判断)

select *from studentwhere Sdept=( select Sdept from student where Sname='王敏')

【例】查询每个学生超过该课程最低分的课程号。(同类课程不是最低分的),子查询的结果返回一个数的时候,这个子查询就可以当一个数用?可以使用in符号,或者大于小于符号。

select Cnofrom SC awhere Grade> ( select min(Grade) from SC b where a.Cno=b.Cno)

【例】查询每个学生超过他选修课程平均成绩的课程号。

select Cnofrom SC awhere Grade> ( select avg(Grade) from SC b where a.Sno=b.Sno)

(3)带有ANY或ALL谓词的子查询

  • ANY表示任何一个,ALL表示所有,可以用在子查询的括号前面

【例】查询其他系中比计算机系某一学生年龄小的学生姓名,性别、年龄和所在系。

select Sname as 姓名,Ssex as 性别, Sage as 年龄, Sdept as 所在系from studentwhere Sage <( select Sage from student where Sdept='CS');

【例】查询其他系中比计算机系所有年龄都小的学生姓名和年龄。

select Sname as 姓名, Sage as 年龄from studentwhere Sdept<>'CS' and Sage <ALL ( select Sage from student where Sdept='CS');

(4 )带有Exists谓词的子查询

【例】查询所有选修了1号课程的学生姓名。

select Sname as 姓名from studentwhere Exists ( select * from SC where Cno=1 and Sno=Student.Sno);

4、集合查询

(1)并UNION

【例】 查询计算机系的学生及年龄不大于19岁的学生详细信息。

select *from studentwhere student.Sdept='CS'unionselect *from studentwhere student.Sage<=19;

(2)交INTERSECT

【例】查询选修了1号课程的与年龄不大于19岁的 学生 详细信息 的交集。

Select *from student,SCwhere student.Sno=SC.Sno and SC.Cno=1INTERSECTSelect *from studentwhere student.Sage<=19;

(3)差EXCEPT

【例】查询计算机科学系的学生与年龄不大于19岁的学生详细信息的差集。

select *from studentwhere student.Sdept='SC'EXCEPTselect *from studentwhere student.Sage<=19;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接


  • 上一条:
    MySQL执行计划的深入分析
    下一条:
    MySQL在线DDL gh-ost使用总结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客