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

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

1,用于设定所select出来的数据是否允许出现重复行(完全相同的数据行)

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

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

2,where:条件

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

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

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

da3e856c41843dbc04211771d6f6f3d9.png

8e7c8d1120be1c7213fe374db266f454.png    

f0c419618cf842a2f6efb412870adede.png 

774aaaa8b404d9a208e274da9815ac6e.png

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

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

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

42990e8c6f38e3f3071639469b37eb79.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;

48028dfe95e10f1c166564b9c45494b4.png

2,查询所有学生的Sno、Cname和Degree列

--找到student的sno

select sno from student;

d98be5aca63c53e38ef8fd6ae3ad7bab.png

--找到score 的sno,degree,cno

select sno,degree,cno from score;

139cb3098dfec25d63b0a534dba2e9fd.png

--找到course的cno

select cno,cname from course;

cc3b71a35c7ef77f47273735330a61a3.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;

932f936d24ec0f1e2a26894e46e08615.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;

31d0217013abf86a3ec738ef9ba4c02d.png

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

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

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

3e92a962f93f7c88de41c37d4492224e.png

--取出score中sno degree

select sno,degree from score;

337d826fa8b022f0e3d0005485005772.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;

b7789eb3a5b5e79b060f9f0af7f2ffa0.png

4,查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

--将109号的成绩取出

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

189224f065585cb7f1d4115cf112938f.png

-- 得出最终表

select * from score

where

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

and cno='3-105';

bbac2ef77b920f4e61f036d2a815fb2c.png

5,查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列

--找到student中sno为108 的Sbirthday

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

05aeaa2baf7f196b61c0d1aecb9d2058.png

-- 得出最终表

select sno,sname,sbirthday from student

where

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

33fb09a846b7412b15a38488c95f11ba.png

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

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

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

4a90d9a6a882125242bf9ca2e2a96685.png

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

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

4041d77516978d6c68be604ccb759d7c.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='张旭'));

99f3a7bcb8ffecef3444246c2862e857.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;

77417c9c6258a1446ef796b547ccd8ff.png

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

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

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

51ca480b2dfdfff60aff699193bd125d.png

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

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

b5ffd0a83128b55382a4b268d44cfd60.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;

a21ebbca62ffbb29afb2b606e2366867.png

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

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

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

38d04c2348498aaad774241bb951c7bf.png

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

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

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

ceb041f0747db6c2b7e7ab5aec906b15.png

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

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

0b0948dae11615ba3dd1a2063ec87060.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);

a7114ee9a84dc9b02b20a7715cdd8cda.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));

718a250b3125c89f569ffd13ef40d714.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;

67da26b36cecc30801dbdb7c36d79604.png

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

select cno,degree from score where degree>85;

46f37395a823e8cb329b0a8ee6657e1b.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;

f2b9711026f4517144fd6a364b6a167b.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;

902b529257de2c2619ab21677bbd9e57.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;

72d99ff67a4699571fc715616aea0514.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='女';

844e13273ed7a29c434e01996241bc39.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

e06dbfa3ae248d5a4d1975b6b555777b.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;

57ac4e3ccd896c2f053ccd2a73cc4051.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;

a4066e1e15671f2ebb51f056d64ba67a.png

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

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

a60ff2957edd408b104918f7c6be0b40.png

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

a946c8ac589e5d542ca7d99ccf003fc8.png

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

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

fd4f1a6ad2492bbd86e5f7dd9588ca7a.png

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

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

2a9728db64c396a6c161a863f7190af2.png

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

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

af02656eb9cc8971ef3b37986dce6430.png

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

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

d332564683f2b177ffc54f46ca1fc38f.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;

b21c8a05f7e45ebfd14092a2a1d21b65.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值