2025年mysql连表条件查询_mysql带条件查询,联表查询

mysql连表条件查询_mysql带条件查询,联表查询恢复内容开始 mysql 1 用于设定所 select 出来的数据是否容许出现重复行 彻底相同的数据行 sql all 容许出现 默认不写就是 All 容许的 spa distinct 不容许出现 就是所谓的 消除重复行 3d 2 where 条件 blog 3 group by

大家好,我是讯享网,很高兴认识大家。

---恢复内容开始---mysql

1,用于设定所select出来的数据是否容许出现重复行(彻底相同的数据行)sql

all:容许出现——默认不写就是All(容许的)。spa

distinct:不容许出现——就是所谓的“消除重复行”3d

2,where:条件blog

3,group by:分组依据 后面加表的字段名,一般只进行一个字段的分组排序

mysql表查询语法形式:select [all | distinct] 字段名或表达式 from 表名 [where] [group by] [having] [order by] [limit];it

练习题:共有下面四张表    学生表:student  教师表:teacher  课程表:course 成绩表:scoreio

73ba833a436b33a716d91d6f4ca3faa8.png
讯享网

070c1d085e6bf2b860a9d0c2869ed631.png    

20f2d7ff0c8dff2b324ed82e52a419db.png 

3d7966c8e429b5b177a4d563b66010b0.png

1,查询Score表中至少有5名学生选修的并以3开头的课程的平均分数class

--操做表score,以cno分组而且cno是以3开头,取出各个cno的总数,及与cno对应的degree的平均值date

select count(cno),avg(degree) from score where cnolike '3%' group by cno;

017b2866a7e9bb2439d1f6f1d09330cf.png

--从上面的虚拟表中找到cno总数大于5的

select * from

(select count(cno) a,avg(degree) b from score where cno like '3%' group by cno) c

where a>5;

1d7ff3c680af19012d05c5ddf8017abb.png

2,查询全部学生的Sno、Cname和Degree列

--找到student的sno

select sno from student;

e4ada647dd37beab278e9eefb156c5a9.png

--找到score 的sno,degree,cno

select sno,degree,cno from score;

b0ec3873caae52850c5a1d7407a59822.png

--找到course的cno

select cno,cname from course;

4f74b8f4d3bb45cea56e896b4f11d06e.png

--组成新表cno sno degree

select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno;

5279c2ff4855a3bf1a51a432fd23b673.png

--组成有cname cno sn degree sname的表

select d.cname,e.cno,e.sno,e.degree,e.sname

from

(select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno) as e

join

(select cname,cno from course) as d

on d.cno=e.cno;

073b7cf59bc725561304c241db47baa6.png

3,查询“95033”班学生的平均分

--从student取sno class,条件是class是95033的

select sno,class from student where class='95033';

9d4b0a90b66ee972e44a45d86c31cef6.png

--取出score中sno degree

select sno,degree from score;

0bb5265facd835fe56415e6d8d0ec35f.png

--将上面两张表组成一张,取degree的平均值

select avg(degree) from

(select sno,class from student where class='95033') a

join

(select sno,degree from score) b

on a.sno=b.sno;

bb45da192231f5df9b57f21ae03d9c71.png

4,查询选修“3-105”课程的成绩高于“109”号同窗成绩的全部同窗的记录

--将109号的成绩取出

select degree from score where sno='109' and cno='3-105';

976cceb41f50397991f989afdab15a8f.png

-- 得出最终表

select * from score

where

degree>(select degree from score where sno='109' and cno='3-105')

and cno='3-105';

e17eca781682d18acb5ec33bbe4624ee.png

5,查询和学号为108的同窗同年出生的全部学生的Sno、Sname和Sbirthday列

--找到student中sno为108 的Sbirthday

select year(sbirthday) from student where sno='108';

37b30355b763b5eb0260d1f1a48a11b8.png

-- 得出最终表

select sno,sname,sbirthday from student

where

year(sbirthday)=(select year(sbirthday) from student where sno='108');

cfc13d9235930752a84a35e58a53c933.png

6.查询“张旭“教师任课的学生成绩(姓名)

--找到teacher中tname为张旭的tno

select tno from teacher where tname='张旭';

4e9dee85968beeda3c0f0242dae05aa7.png

--找到course中tno和teacher中tname为张旭的tno相同的cno

select cno from course where tno=(select tno from teacher where tname='张旭');

42eb2cb951d670053d955d841907cc04.png

--找到score中cno为6-166的sno cno degree

select sno,cno,degree from score

where

cno=(select cno from course where tno=(select tno from teacher where tname='张旭'));

d3eab8ea83e22034d9de2f8df28fe975.png

--找到student表中与上表sno相同的sname与上表组成新表

select a.sname,b.sno,b.cno,b.degree from

(select sno,sname from student) a

join

(select sno sno,cno,degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='张旭'))) b

on a.sno=b.sno;

22fb364ce6cf688fd4cb3758481304ac.png

7,查询考计算机导论的学生成绩

--找到course中cname为计算机导论的cno

select cno from course where cname='计算机导论';

53d683af5b368d8bb86cebc6eae283b7.png

--找到score中cno与上表中相同时的sno degree

select sno,degree from score where cno=(select cno from course where cname='计算机导论');

5606762535149ea49cbc684a8f94b458.png

-- 得出最终表

select b.sname,a.sno,a.degree from

(select sno,degree from score where

cno=(select cno from course where cname='计算机导论')) a join (select sname,sno from student) b

on a.sno=b.sno;

e356b9fbaadb25c5d3fdb7a59e7c342c.png

8,查询李诚老师教的课程名称

select tno from teacher where tname='李诚';

select cname,cno from course where tno=(select tno from teacher  where tname='李诚');

d2c283f5a15dcb9c034b256f41efdb58.png

9,查询选修某课程的同窗人数多于5人的教师姓名

--score以cno分组,统计cno的数量

select count(cno) a,cno from score group by cno;

7dcbfb1deba394f154de38542a86fa74.png

--找到上表中cno数量大于5的的cno

select cno from (select count(cno) a,cno from score group by cno) b where a>5;

875554921281da0ad21fff428b9b6e1a.png

--找到与上表cno一致的表course表中的tno

select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5);

34577af3f25076525d679702319f4872.png

--与上表tno一致的表teacher中的tname

select tname from teacher where tno=(select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5));

57707fdcf89607320819bbe55f35911b.png

10,查询95033班和95031班全体学生的记录

--找到表score中的sno和cno 和degree

select sno,cno,degree from score;

--将上表和student组合

select a.sno,a.sname,a.ssex,a.sbirthday,a.class,b.cno,b.degree from

(select * from student) a join (select cno,degree,sno from score) b

on a.sno=b.sno;

bb5a28cb74c7867f1484ba3a10eb2e20.png

11,查询存在有85分以上成绩的课程Cno

select cno,degree from score where degree>85;

80c32da54421434a2d9e170db905757b.png

12,查询出“计算机系“教师所教课程的成绩表

--找到teacher中的tno tname

select tno,tname from teacher where dapart='计算机系';

--找到course中tno和上表相同的cno

select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='计算机系') b on a.tno=b.tno;

--找到score中cno与上表相同的degree

select c.sno,c.cno,c.degree,d.tname from (select sno,cno,degree from score) c join (select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='计算机系') b on a.tno=b.tno) d on c.cno=d.cno;

958b470cfe4343f90401e705ef506671.png

13,查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同窗    的Cno、Sno和Degree,并按Degree从高到低次序排序

--score中找到cno=3-245的degree,取出最小值

select min(degree) from score where cno='3-245';

--score中找到cno=3-105的cno sno degree,而且degree大于上表的degree

select cno,sno,degree from

score where cno='3-105'

and degree>(select min(degree) from score where cno='3-245')

order by degree desc;

01d56490e2a04a52a5527f13a78e0470.png

14,查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同窗的    Cno、Sno和Degree.

--找到score中cno=3-245中的degree的最大值

select max(degree) from score where cno='3-245';

--找到score中cno=3-105的cno sno degree

select cno,sno,degree from

score where

degree>(select max(degree) from score where cno='3-245') order by degree asc;

c59348ecaa00c4f9c94f6a3037dbfcce.png

15,查询全部教师和同窗的name、sex和birthday

select tname,tsex,tbirthday from teacher

union

select sname,ssex,sbirthday from student;

16,查询全部“女”教师和“女”同窗的name、sex和birthday

select tname,tsex,tbirthday from teacher where tsex='女'

union

select sname,ssex,sbirthday from student where ssex='女';

ecc67671cf0e6640af1ccab7c694bcc3.png

17,查询成绩比该课程平均成绩低的同窗的成绩表

select avg(degree) from score where cno='3-105';

select avg(degree) from score where cno='3-245';

select avg(degree) from score where cno='6-166';

select * from score where cno='3-105' and degree

union

select * from score where cno='3-245' and degree

union

select * from score where cno='6-166' and degree

8d21b69b686c374972dccb0622d6183c.png

18,查询全部任课教师的Tname和Depart

select a.tname,a.dapart from

(select tno,tname,dapart from teacher) a join

(select tno from course) b

on a.tno=b.tno;

b2e55b28d3390d1a36ce374d957dbea7.png

19,查询全部未讲课的教师的Tname和Depart

select a.tname,a.dapart from

(select tno,tname,dapart from teacher) a join

(select tno from course) b

on a.tno=b.tno;

98e10a9980e288e1147669382b6ca5db.png

20,查询至少有2名男生的班号

select count(ssex),class from student where ssex='男' group by class;

ac9a90b17a157b30760b220bb707227a.png

select b.class from (select count(ssex) a,class from student where ssex='男' group by class) b where a>=2;

1f53921066d68d212a269585639f38a8.png

21,查询Student表中不姓“王”的同窗记录

select * from student where sname not like '王%';

bb07d6da8648633c2ed86204192a24cb.png

22,查询Student表中每一个学生的姓名和年龄

select  sname,year(now())-year(sbirthday) from student;

f50cf9b5f0c954fbe2fc80959ab52aa2.png

23,查询Student表中最大和最小的Sbirthday日期值

select min(day(sbirthday)),max(day(sbirthday)) from student;

8c0a03f33c968db9d82a348eb179da15.png

24,以班号和年龄从大到小的顺序查询Student表中的所有记录

select * from student order by class desc,date(sbirthday) asc;

54e871eb9d17d1e18af4f21c88c3e6f6.png

25,查询“男”教师及其所上的课程

select tno from teacher where tsex='男';

select a.tno,b.cname,a.tname from (select tno,tname from teacher where tsex='男') a join (select * from course) b on a.tno=b.tno;

f931e20fa43567eefdcfab1efad3c0b5.png

小讯
上一篇 2025-03-20 07:37
下一篇 2025-01-11 16:55

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/117377.html