当前位置:网站首页>【mysql实战】查询语句实战演示
【mysql实战】查询语句实战演示
2022-06-27 19:41:00 【宋丹敏】
简单查询
学生选课数据库查询语句实例:
- select form where
use 学生选课;
SELECT sno,sname FROM student
WHERE sdept = '数计学院'
SELECT DISTINCT sno FROM SC
SELECT sno,grade FROM SC
WHERE cno = 10001
ORDER BY grade DESC,sno ASC
SELECT sno,grade*0.8 FROM SC
WHERE cno = 10001 AND (grade < 70 OR grade >60)
SELECT* FROM student
WHERE sdept IN ('数计学院','生工学院')
ORDER BY sdept DESC
SELECT sno,cno FROM SC
WHERE grade IS NULL
SELECT SC.sno,sname,cname,grade FROM SC,Course,student
WHERE SC.sno = student.sno AND SC.cno =Course.cno
ORDER BY grade DESC
SELECT* FROM Course
WHERE cno IN (SELECT cno FROM SC WHERE sno IN(SELECT sno FROM student WHERE sdept = '数计学院' )
AND ccredit > 2)
SELECT student.*,SC.* FROM student,Sc
WHERE sc.sno=student.sno
SELECT X.sno,sname,grade FROM student X,SC Y
WHERE X.sno=Y.sno AND Y.cno = '10001'
SElECT FIRST.*,SECOND.cpno 先行课的先行课 FROM Course FIRST,Course SECOND
WHERE (FIRST.cpno=SECOND.cname AND SECOND.cpno IS NOT NULL)
嵌套查询
- left join 左连接
--嵌套
/*
1.选修课里没有UML故修改为java
查询了选修了java的课程的学生学号和姓名
*/
--method1.
select sno ,sname from student where sno in
(select sno from SC where cno in (select cno from Course where cname = 'java'))
--method2.
select student.sno ,sname from student ,SC,Course where student.sno=SC.sno and SC.cno = Course.cno and Course.cname = 'java'
/*
2.查询比王华年龄大的学生的学号和姓名。
*/
select sno,sname from student where sage >
(select sage from student where sname ='王华')
/*
3.查询c1课程的成绩低于张三的学生学号和成绩
*/
select SC.sno ,grade from SC where grade <
(select grade from SC,student where (SC.sno=student.sno and student.sname='zeze' and SC.cno='c1'))
/*
4.查询其他学院中比数计学院学生年龄都小的学生
*/
select sname from student where sage >all (select sage from student where sdept ='数计学院') AND sdept!='数计学院'
/*
5查询选修了C2的课程的学生姓名
*/
--method1
select student.sname from student,sc where student.sno=SC.sno and SC.cno= 'c1'
--method2
select student.sname from student left join SC on student.sno=SC.sno left join Course on Course.cno=SC.cno where Course.cno='c1 '
/*
6.查询了选修了全部课程的学生姓名
*/
select sname
from student
where not exists (select *
from Course
where not exists( select *
from SC
where SC.sno = student.sno and SC.cno = Course.cno))
/*
7.查询至少选修了学生为"S2"的学生所选秀的全部课程的学生学号的姓名
*/
select sname
from student
where not exists(select *
from SC
where SC.sno = 'S2' and not exists(select *
from Course
where SC.sno = student.sno and SC.cno = Course.cno))
/*
9.查询 既选修了数据结构又选修了数据库原理与应用的课程的学生姓名(ps:因为表中没有,所以没有结果)
*/
select sname from student
left join SC sc
on sc.sno = student.sno
left join Course c1
on sc.cno = c1.cno
where c1.cname = '数据结构'
intersect
select sname from student
left join SC sc
on sc.sno = student.sno
left join Course c1
on sc.cno = c1.cno
where c1.cname = '数据库原理'
组合查询
- 交集 intersect
- 并集 union
- 除去 expcept
- 分组 group by
- 过滤 聚合having 与group by 一起使用
--组合
/*
1.使用集合运算查询既选修了数据结构课程又选修了数据库原理及应用课程的学生姓名。
*/
select sname
from student
where sno in( select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构'))
intersect
select sname
from student
where sno in( select sno
from SC
where cno in (select cno
from Course
where cname = '数据库原理'))
/*
2.使用集合运算查询选修了数据结构课程或选修了数据库原理及应用课程的学生学号。
*/
select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构')
union
select sno
from SC
where cno in (select cno
from Course
where cname = '数据库原理')
/*
3.使用集合运算查询选修了数据结构课程而没有选修了数据库原理及应用课程的学生学号。
*/
select sno
from SC
where cno in (select cno
from Course
where cname = '数据结构')
except
select sno
from SC
where sno in (select cno
from Course
where cname = '数据库原理')
/*
4.统计选修了课程的学生人数。
*/
select count(distinct sno) as status
from SC
/*
5.查询选修成绩合格的课程超过 4 门以上学生的学生学号、总成绩。
(ps:having 也是一种过滤,是聚合过滤。
where 为过滤,在结果集产生前过滤。
having 在 where对源输入过滤之后聚合的结果再行过滤。
HAVING子句可以让我们筛选成组后的各组数据.
WHERE子句在聚合前先筛选记录.也就是说作用在GROUP BY 子句和HAVING子句前.
而 HAVING子句在聚合后对组记录进行筛选。 )
*/
select SC.sno,SUM(grade) as '总成绩'
from SC
where SC.grade > =60
group by sno
having count(cno)>4
/*
6.统计各院系的学生人数。
*/
select sdept ,count(sdept) as total from student
group by sdept
/*
7.统计各年龄的学生的人数
*/
select sage ,count(sage) as everyone from student
group by sage
/*
8.统计每个学生的选修课数目和平均成绩
*/
select sno,count(cno) as '课程数目',AVG(grade) as '平均成绩'
from SC
group by sno
/*
9.查询每门课程的详细信息及选课人数。
*/
select Course.*,count(SC.cno) as '选课人数'
from Course,SC
where SC.cno = Course.cno
group by Course.cno,ccredit,cname,cpno,ctech
持续补充中…
边栏推荐
- This set of steps for performance testing using JMeter includes two salary increases and one promotion
- qt base64加解密
- Luogu p5706 redistributing fertilizer and house water
- Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)
- 如何做好功能测试?你确定不想知道吗?
- [LeetCode]动态规划解分割数组II[Arctic Fox]
- Special tutorial - Captain selection game
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- 管理系統-ITclub(下)
- Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?
猜你喜欢

∫(0→1) ln(1+x) / (x ² + 1) dx

I think I should start writing my own blog.

美团20k软件测试工程师的经验分享

Read write separation master-slave replication of MySQL

Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移

Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app

Go from introduction to actual combat - task cancellation (note)

VMware virtual machine PE startup

我想我要开始写我自己的博客了。

登录凭证(cookie+session和Token令牌)
随机推荐
Read write separation master-slave replication of MySQL
Selenium上传文件有多少种方式?不信你有我全!
Hash table - sum of arrays
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
美团20k软件测试工程师的经验分享
Gbase 8A OLAP analysis function cume_ Example of dist
Knowledge sorting of exception handling
[LeetCode]186. 翻转字符串里的单词 II
Go 访问GBase 8a 数据库的一个方法
[LeetCode]515. 在每个树行中找最大值
Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
Simulink导出FMU模型文件方法
Special tutorial - Captain selection game
VMware virtual machine PE startup
Experience sharing of meituan 20K Software Test Engineers
軟件測試自動化測試之——接口測試從入門到精通,每天學習一點點
管理系统-ITclub(上)
QT base64 encryption and decryption
Little known MySQL import data
开源技术交流丨一站式全自动化运维管家ChengYing入门介绍