mysql查询语句4,Mysql学习笔记4(查询语句)

准备:创建以下表格

学生表Student

学号

姓名

性别

出生年月日

所在班级

create table student(

sno varchar(20) primary key,

sname varchar(20) not null,

ssex varchar(10) not null,

sbirthday datetime,

class varchar(20)

);

教师表Teacher

教师编号

教师名字

教师性别

出生年月日

职称

所在部门

create table teacher(

tno varchar(20) primary key,

tname varchar(20) not null,

tsex varchar(10) not null,

tbirthday datetime,

prof varchar(20) not null,

depart varchar(20) not null

);

课程表Course

课程号

课程名称

教师编号

create table course(

cno varchar(20) primary key,

cname varchar(20) not null,

tno varchar(20) not null,

foreign key(tno) references teacher(tno)

);

成绩表Score

学号

课程号

成绩

create table score(

sno varchar(20) not null,

cno varchar(20) not null,

degree decimal,

foreign key(sno) references student(sno),

foreign key(cno) references course(cno),

primary key(sno, cno)

);

添加数据

# 添加学生信息

insert into student values('101', '曾华', '男', '1977-09-01', '95033');

insert into student values('102', '匡明', '男', '1975-10-02', '95031');

insert into student values('103', '王丽', '女', '1976-01-23', '95033');

insert into student values('104', '李军', '男', '1976-02-20', '95033');

insert into student values('105', '王芳', '女', '1975-02-10', '95031');

insert into student values('106', '陆君', '男', '1974-06-03', '95031');

insert into student values('107', '王尼玛', '男', '1976-02-20', '95033');

insert into student values('108', '张全蛋', '女', '1975-02-10', '95031');

insert into student values('109', '赵铁柱', '男', '1974-06-03', '95031');

# 添加教师表

insert into teacher values('804', '李诚', '男', '1958-12-02', '副教授', '计算机系');

insert into teacher values('856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');

insert into teacher values('825', '王萍', '女', '1972-05-05', '助教', '计算机系');

insert into teacher values('831', '刘冰', '女', '1977-08-14', '助教', '电子工程系');

# 添加课程表

insert into course values('3-105', '计算机导论', '825');

insert into course values('3-245', '操作系统', '804');

insert into course values('6-166', '数字电路', '856');

insert into course values('9-888', '高等数学', '831');

# 添加成绩表

insert into score values('103', '3-105', '92');

insert into score values('103', '3-245', '86');

insert into score values('103', '6-166', '85');

insert into score values('105', '3-105', '88');

insert into score values('105', '3-245', '75');

insert into score values('105', '6-166', '79');

insert into score values('109', '3-105', '76');

insert into score values('109', '3-245', '68');

insert into score values('109', '6-166', '81');

查询练习

1.查询student表的所有记录

select * from student;

# * 取所有列

15e336689bba

1.png

2.查询student表的所有记录的sname、ssex和class列

select sname, ssex, class from student;

15e336689bba

2.png

3.查询教师所有的单位,即不重复的depart列

排除重复: distinct

select distinct depart from teacher;

15e336689bba

3.png

4.查询score表中成绩在60-80之间的所有记录

查询区间: between...and...

注:不同数据库对边界问题处理不一

select * from score where degree between 60 and 80;

#方法二:使用运算符比较

select * from score where degree > 60 and where degree < 80;

15e336689bba

4.png

5.查询score表中成绩为85,86或88的记录

表示或者关系的查询: in

select * from score where degree in(85, 86, 88);

15e336689bba

5.png

6.查询student表中“95031”班或性别为“女”的同学记录

不同字段的“或”: or

select * from student where class='95031' or ssex='女';

15e336689bba

6.png

7.以class降序查询student表中的所有记录

升序:默认

降序:desc

select * from student order by class desc;

# 删掉desc默认为升序

15e336689bba

7.png

8.以cno升序、degree降序查询score表的所有记录

select * from score order by cno asc, degree desc;

15e336689bba

8.png

9.查询“95031”班的学生人数

统计:count

select count(*) from student where class='95031';

15e336689bba

9.png

10.查询score表中的最高分的学生学号和课程号(子查询或者排序)

select sno, cno from score where degree=(select max(degree) from score);

# 解析:

## step1:找到最高分

select max(degree) from score

## step2:找最高分的学生学号和课程号

15e336689bba

10.png

# 使用排序的方法

select sno, cno, degree from score order by degree desc limit 0, 1;

# limit 0, 1 取第一条

where degree=(select max(degree) from score);

# 但排序的做法有缺陷,如果存在两个最高分会识别不出

11.查询每门课的平均成绩

分组:group by

select avg(degree) from score where cno='3-105'; #计算一门课的平均成绩

select cno,avg(degree) from score group by cno;

15e336689bba

11.png

12.查询score表中至少有2名学生选修的并以3开头的课程的平均成绩

select cno,avg(degree) ,count(*) from score

group by cno

having count(cno)>=2

and cno like '3%'; # 以3开头

# 先分组后条件用having,先加条件后分组用where

# count(*) 查询有几个人

15e336689bba

12.png

13.查询分数大于70,小于90的sno列

select sno, degree from score

where degree>70 and degree<90;

# 或:

select sno, degree from score

where degree between 70 and 90;

15e336689bba

13.png

14.查询所有学生的sname、cno和degree列

select sname,cno,degree from student,score

where student.sno=score.sno;

15e336689bba

14.png

15.查询所有学生的sno、cname和degree列

select cno,cname from course;

+-------+-----------------+

| cno | cname |

+-------+-----------------+

| 3-105 | 计算机导论 |

| 3-245 | 操作系统 |

| 6-166 | 数字电路 |

| 9-888 | 高等数学 |

+-------+-----------------+

select cno,sno,degree from score;

+-------+-----+--------+

| cno | sno | degree |

+-------+-----+--------+

| 3-105 | 103 | 92 |

| 3-245 | 103 | 86 |

| 6-166 | 103 | 85 |

| 3-105 | 105 | 88 |

| 3-245 | 105 | 75 |

| 6-166 | 105 | 79 |

| 3-105 | 109 | 76 |

| 3-245 | 109 | 68 |

| 6-166 | 109 | 81 |

+-------+-----+--------+

select sno,cname,degree from course,score

where course.cno = score.cno;

15e336689bba

15.png

16.查询所有学生的sname、cname和degree列

select sname,cname,degree from student,course,score

where student.sno=score.sno

and course.cno=score.cno;

15e336689bba

16.png

17.查询"95031"班学生每门课的平均分

select * from student where class='95031';

+-----+-----------+------+---------------------+-------+

| sno | sname | ssex | sbirthday | class |

+-----+-----------+------+---------------------+-------+

| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |

| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |

| 106 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |

| 108 | 张全蛋 | 女 | 1975-02-10 00:00:00 | 95031 |

| 109 | 赵铁柱 | 男 | 1974-06-03 00:00:00 | 95031 |

+-----+-----------+------+---------------------+-------+

select * from score where sno in

(select sno from student where class='95031');

+-----+-------+--------+

| sno | cno | degree |

+-----+-------+--------+

| 105 | 3-105 | 88 |

| 105 | 3-245 | 75 |

| 105 | 6-166 | 79 |

| 109 | 3-105 | 76 |

| 109 | 3-245 | 68 |

| 109 | 6-166 | 81 |

+-----+-------+--------+

select cno,avg(degree) from score

where sno

in (select sno from student where class='95031')

group by cno;

15e336689bba

17.png

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

“所有选修3-105课程的同学中,哪些是高于109号同学的成绩”

select degree from score

where sno='109' and cno='3-105';

+--------+

| degree |

+--------+

| 76 |

+--------+

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

degree>(select degree from score

where sno='109' and cno='3-105');

15e336689bba

18.png

19.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录

select * from score

where degree>(select degree from score where

sno='109' and cno='3-105');

15e336689bba

19.png

20.查询和学号为108、101的同学同年出生的所有学生的sno、sname和sbirthday列

select year(sbirthday) from student where sno in (108,101);

+-----------------+

| year(sbirthday) |

+-----------------+

| 1977 |

| 1975 |

+-----------------+

select sno,sname,sbirthday from student

where year(sbirthday) in

(select year(sbirthday) from student where sno in (108,101));

15e336689bba

20.png

21.查询"张旭"教师任课的学生成绩

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

+-----+--------+------+---------------------+--------+-----------------+

| tno | tname | tsex | tbirthday | prof | depart |

+-----+--------+------+---------------------+--------+-----------------+

| 856 | 张旭 | 男 | 1969-03-12 00:00:00 | 讲师 | 电子工程系 |

+-----+--------+------+---------------------+--------+-----------------+

select * from course

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

+-------+--------------+-----+

| cno | cname | tno |

+-------+--------------+-----+

| 6-166 | 数字电路 | 856 |

+-------+--------------+-----+

select * from score where cno=

(select cno from course

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

15e336689bba

21.png

22.查询选修某课程的同学人数多余5人的教师姓名

select * from score;

+-----+-------+--------+

| sno | cno | degree |

+-----+-------+--------+

| 103 | 3-105 | 92 |

| 103 | 3-245 | 86 |

| 103 | 6-166 | 85 |

| 105 | 3-105 | 88 |

| 105 | 3-245 | 75 |

| 105 | 6-166 | 79 |

| 109 | 3-105 | 76 |

| 109 | 3-245 | 68 |

| 109 | 6-166 | 81 |

+-----+-------+--------+

#发现没有多于5个的课程

#添加数据:

insert into score values ('101', '3-105', '90');

insert into score values ('102', '3-105', '91');

insert into score values ('104', '3-105', '89');

select cno from score group by cno having count(*)>5;

+-------+

| cno |

+-------+

| 3-105 |

+-------+

select tno from course where cno=

(select cno from score group by cno having count(*)>5);

+-----+

| tno |

+-----+

| 825 |

+-----+

select tname from teacher where tno=

(select tno from course where cno=

(select cno from score group by cno having count(*)>5));

+--------+

| tname |

+--------+

| 王萍 |

+--------+

23.查询95033班和95031班全体学生的记录

insert into student values('110', '张飞', '男', '1977-09-01', '95038');

select * from student where class in ('95033', '95031');

+-----+-----------+------+---------------------+-------+

| sno | sname | ssex | sbirthday | class |

+-----+-----------+------+---------------------+-------+

| 101 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |

| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |

| 103 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |

| 104 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |

| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |

| 106 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |

| 107 | 王尼玛 | 男 | 1976-02-20 00:00:00 | 95033 |

| 108 | 张全蛋 | 女 | 1975-02-10 00:00:00 | 95031 |

| 109 | 赵铁柱 | 男 | 1974-06-03 00:00:00 | 95031 |

+-----+-----------+------+---------------------+-------+

24.查询存在有85分以上成绩的课程cno

select cno from score where degree>85 group by cno;

+-------+

| cno |

+-------+

| 3-105 |

| 3-245 |

+-------+

25.查询"计算机系"所有教师所教课程的成绩表

select tno from teacher where depart='计算机系';

+-----+

| tno |

+-----+

| 804 |

| 825 |

+-----+

select cno from course where tno in

(select tno from teacher where depart='计算机系');

+-------+

| cno |

+-------+

| 3-245 |

| 3-105 |

+-------+

select * from score where cno in

(select cno from course where tno in

(select tno from teacher where depart='计算机系'));

15e336689bba

25.png

26.查询"计算机系"与“电子工程系”不同职称的教师的tname和prof

求并:union

# 并集-交集,这题有点儿问题,不用纠结

select * from teacher;

+-----+--------+------+---------------------+-----------+-----------------+

| tno | tname | tsex | tbirthday | prof | depart |

+-----+--------+------+---------------------+-----------+-----------------+

| 804 | 李诚 | 男 | 1958-12-02 00:00:00 | 副教授 | 计算机系 |

| 825 | 王萍 | 女 | 1972-05-05 00:00:00 | 助教 | 计算机系 |

| 831 | 刘冰 | 女 | 1977-08-14 00:00:00 | 助教 | 电子工程系 |

| 856 | 张旭 | 男 | 1969-03-12 00:00:00 | 讲师 | 电子工程系 |

+-----+--------+------+---------------------+-----------+-----------------+

select * from teacher where depart='计算机系' and prof not in

(select prof from teacher where depart='电子工程系')

union

select * from teacher where depart='电子工程系' and prof not in

(select prof from teacher where depart='计算机系');

15e336689bba

26.png

27.查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”的同学的cno、sno和degree,并按degree从高到低次序排序

select * from score where cno='3-245';

+-----+-------+--------+

| sno | cno | degree |

+-----+-------+--------+

| 103 | 3-245 | 86 |

| 105 | 3-245 | 75 |

| 109 | 3-245 | 68 |

+-----+-------+--------+

select * from score where cno='3-105';

+-----+-------+--------+

| sno | cno | degree |

+-----+-------+--------+

| 101 | 3-105 | 90 |

| 102 | 3-105 | 91 |

| 103 | 3-105 | 92 |

| 104 | 3-105 | 89 |

| 105 | 3-105 | 88 |

| 109 | 3-105 | 76 |

+-----+-------+--------+

# ---至少?大于其中至少一个,any(题目有点儿问题,不用管它)

select * from score

where cno='3-105'

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

order by degree desc;

15e336689bba

27.png

28.查询选修编号为“3-105”课程且成绩高于选修编号为“3-245”的同学的cno、sno和degree

且:all

select * from score

where cno='3-105'

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

29.查询所有教师和同学的name、sex和birthday

别名:as

select sname as name,ssex as sex,sbirthday as birthday from student

union

select tname,tsex,tbirthday from teacher;

15e336689bba

29.png

30.查询所有“女”教师和“女”同学的name、sex和birthday

select tname as name,tsex as sex,tbirthday as birthday from teacher

where tsex='女'

union

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

31.查询成绩比该课程平均成绩低的同学的成绩表

select cno,avg(degree) from score group by cno;

+-------+-------------+

| cno | avg(degree) |

+-------+-------------+

| 3-105 | 87.6667 |

| 3-245 | 76.3333 |

| 6-166 | 81.6667 |

+-------+-------------+

select * from score as a where degree<

(select avg(degree) from score as b where a.cno=b.cno);

# 这里把score表复制一份,两份表分别为a 和b

15e336689bba

31.png

32.查询所有任课教师的tname和depart

select * from teacher;

+-----+--------+------+---------------------+-----------+-----------------+

| tno | tname | tsex | tbirthday | prof | depart |

+-----+--------+------+---------------------+-----------+-----------------+

| 804 | 李诚 | 男 | 1958-12-02 00:00:00 | 副教授 | 计算机系 |

| 825 | 王萍 | 女 | 1972-05-05 00:00:00 | 助教 | 计算机系 |

| 831 | 刘冰 | 女 | 1977-08-14 00:00:00 | 助教 | 电子工程系 |

| 856 | 张旭 | 男 | 1969-03-12 00:00:00 | 讲师 | 电子工程系 |

+-----+--------+------+---------------------+-----------+-----------------+

select * from course;

+-------+-----------------+-----+

| cno | cname | tno |

+-------+-----------------+-----+

| 3-105 | 计算机导论 | 825 |

| 3-245 | 操作系统 | 804 |

| 6-166 | 数字电路 | 856 |

| 9-888 | 高等数学 | 831 |

+-------+-----------------+-----+

select tname,depart from teacher

where tno in (select tno from course);

+--------+-----------------+

| tname | depart |

+--------+-----------------+

| 李诚 | 计算机系 |

| 王萍 | 计算机系 |

| 刘冰 | 电子工程系 |

| 张旭 | 电子工程系 |

+--------+-----------------+

33.查询至少有2名男生的班号

select class from student

where ssex='男'

group by class having count(*)>1;

+-------+

| class |

+-------+

| 95033 |

| 95031 |

+-------+

34.查询student表中不姓“王”的同学记录

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

35.查询student表中每个学生的姓名和年龄

年龄:当前年份-出生年份

当前年份:year(now())

select sname,year(now())-year(sbirthday) as '年龄' from student;

+-----------+--------+

| sname | 年龄 |

+-----------+--------+

| 曾华 | 43 |

| 匡明 | 45 |

| 王丽 | 44 |

| 李军 | 44 |

| 王芳 | 45 |

| 陆君 | 46 |

| 王尼玛 | 44 |

| 张全蛋 | 45 |

| 赵铁柱 | 46 |

| 张飞 | 43 |

+-----------+--------+

36.查询student表中最大最小的sbirthday日期值

max

min

select max(sbirthday) as '最大', min(sbirthday) as '最小' from student;

+---------------------+---------------------+

| 最大 | 最小 |

+---------------------+---------------------+

| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |

+---------------------+---------------------+

#根据数字大小选取

37.以班号和年龄从大到小的顺序查询student表中的全部记录

select * from student order by class desc,sbirthday;

15e336689bba

37.png

38.查询"男"教师及其所上的课程

select * from course where tno in

(select tno from teacher where tsex='男');

39.查询最高分同学的sno、cno和degree列

select sno,cno,degree from score

where degree = (select max(degree) from score);

40.查询和“李军”同性别的所有同学的sname

select sname from student

where ssex=(select ssex from student where sname='李军');

41.查询和“李军”同性别并同伴的同学sname

select sname from student

where ssex=(select ssex from student where sname='李军')

and class=(select class from student where sname='李军');

42.查询所有选修“计算机导论”课程的“男”同学的成绩表

select * from score

where cno in (select cno from course where cname='计算机导论' )

and sno in

(select sno from student where ssex='男');

43.假设使用以下命令建立了一个grade表:

create table grade(

low int(3),

upp int(3),

grade char(1)

);

insert into grade values(90,100,'A');

insert into grade values(80,89,'B');

insert into grade values(70,79,'C');

insert into grade values(60,69,'D');

insert into grade values(0,59,'E');

查询所有同学的sno、cno和grade列

select sno,cno,grade from score,grade

where degree between low and upp;

15e336689bba

43.png

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值