1、建表

学生表

student
学号,姓名,性别,出生,所在班级
create table student(
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(10) not null,
sbirthday datetime,
sclass 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)
);

2、插入数据

添加学生信息

insert into student value('101','曾华','男','1977-09-01','95033');
insert into student value('102','匡明','男','1975-10-02','95031');
insert into student value('103','王丽','女','1977-01-23','95033');
insert into student value('104','李军','男','1977-02-20','95033');
insert into student value('105','王芳','女','1977-02-10','95031');
insert into student value('106','陆军','男','1977-06-03','95031');
insert into student value('107','李缩','男','1977-02-21','95033');
insert into student value('108','王解','女','1975-02-13','95031');
insert into student value('109','陆稍','男','1977-06-01','95031');

添加教师信息

insert into teacher value('834','李成','男','1958-12-01','副教授','计算机系');
insert into teacher value('856','张旭','男','1968-11-21','讲师','电子工程系');
insert into teacher value('825','王品','女','1972-05-05','助教','计算机系');
insert into teacher value('831','刘冰','女','1977-08-14','助教','电子工程系');

添加课程表

insert into course value('3-245','计算机导论','825');
insert into course value('3-105','操作系统','834' );
insert into course value('6-166','数字电路','856' );
insert into course value('9-888','高等数学','831' );

添加成绩表

insert into score value('103','3-245','86');
insert into score  value('105','3-245','75' );
insert into score value('109','3-245','68' );
insert into score  value('103','3-105','92' );
insert into score value('105','3-105','88');
insert into score  value('109','3-105','76' );
insert into score value('101','3-105','91');
insert into score value('102','3-105','78');
insert into score  value('103','6-166','85' );
insert into score  value('105','6-166','79' );
insert into score  value('109','6-166','81' );

3、表数据查看

学生

+-----+--------+------+---------------------+--------+
| sno | sname  | ssex | sbirthday           | sclass |
+-----+--------+------+---------------------+--------+
| 101 | 曾华   | 男   | 1977-09-01 00:00:00 | 95033  |
| 102 | 匡明   | 男   | 1975-10-02 00:00:00 | 95031  |
| 103 | 王丽   | 女   | 1977-01-23 00:00:00 | 95033  |
| 104 | 李军   | 男   | 1977-02-20 00:00:00 | 95033  |
| 105 | 王芳   | 女   | 1977-02-10 00:00:00 | 95031  |
| 106 | 陆军   | 男   | 1977-06-03 00:00:00 | 95031  |
| 107 | 李缩   | 男   | 1977-02-21 00:00:00 | 95033  |
| 108 | 王解   | 女   | 1975-02-13 00:00:00 | 95031  |
| 109 | 陆稍   | 男   | 1977-06-01 00:00:00 | 95031  |
+-----+--------+------+---------------------+--------+

教师

+-----+--------+------+---------------------+-----------+-----------------+
| tno | tname  | tsex | tbirthday           | prof      | depart          |
+-----+--------+------+---------------------+-----------+-----------------+
| 825 | 王品   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 834 | 李成   | 男   | 1958-12-01 00:00:00 | 副教授    | 计算机系        |
| 856 | 张旭   | 男   | 1968-11-21 00:00:00 | 讲师      | 电子工程系      |
+-----+--------+------+---------------------+-----------+-----------------+

课程

+-------+-----------------+-----+
| cno   | cname           | tno |
+-------+-----------------+-----+
| 3-105 | 操作系统        | 834 |
| 3-245 | 计算机导论      | 825 |
| 6-166 | 数字电路        | 856 |
| 9-888 | 高等数学        | 831 |
+-------+-----------------+-----+

成绩

+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 3-105 |     91 |
| 102 | 3-105 |     78 |
| 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 |
+-----+-------+--------+

4、查询练习

1、查询student表的所有记录
select * from student;
2、查询student表中的所有记录的sname、ssex和sclass列
select sname, ssex, sclass from student;
3、查询教师所有的单位即不重复的depart列
select distinct depart from teacher; // distinct排重
4、查询score表中成绩在60-80之间的所有记录
select * from score where degree between 60 and 80;
select * from score where degree > 60 and degree < 80;
5、查询score表中成绩为85,86或87的记录
select * from score where degree in(85, 86, 87);
6、查询student表中"95031"班,或性别为'女'的同学记录
select * from student where sclass='95031' or ssex='女';
7、以sclass降序查询student表中的所有数据 //desc降序 asc升序默认是升序的一般不会写
select * from student order by sclass desc;
8、以cno升序、degree降序查询score表中的所有记录 //遇到相同的就降序
select * from score order by cno asc ,degree desc;
9、查询"95031"班级的人数
select count(*) from student where sclass = '95031';
10、查询score表中最高分的学生学号和课程号。(子查询或排序)
select sno, cno from score where degree = (select max(degree) from score);
select sno, cno, degree from score order by degree desc limit 0, 1;
如果按照分数排序,select的时候一定要把degree放进去,0表示从第一行开始数一行,如果是1, 2则表示从第二行开始数两条
11、查询每门课的平均成绩
select * from course;
select avg(degree) from score where cno='3-105';
一门一门查询
下面是写在一句话中
select cno, avg(degree) from score group by cno; 
12、查询score表中至少有两名学生选修的并以3开头的课程的平均分数
select cno, avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like '3%';
count(*)表示给出这个group的个数,此题也可以不加
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;
14、查询所有学生的sname, cno和degree列
select sno, sname from student;
select sno, cno, degree from score;
需要用第一句话中的sname替换第二行的sno,下面使用多表查寻语句
select sname, cno, degree from student, score where student.sno=score.sno;
15、查寻所有学生的sno, cname和degree
select sno, cname, degree from course, score where course.cno=score.cno; 
16、查询所有学生的sname、cname、degree列(来自三张表)
select sname, cname, degree from student, course, score where student.sno=score.sno and course.cno=score.cno;
select sname, cname, degree, student.sno as stu_sno, course.cno from student, course, score where student.sno=score.sno and course.cno=score.cno;
因为sno有歧义所以要加student.sno,可以用as去改名
17、查询95031班学生每门课的平均分
select cno, avg(degree) from score where sno in (select sno from student where sclass='95031') group by cno;
18、查询选修“3-105”课程的成绩高于109号同学“3-105”成绩的所有同学记录
select * from score where cno='3-105' and degree>(select degree from score where sno='109' and cno='3-105'); 
19、查询成绩高于学号为"109"、课程号为"3-105"的成绩的所有记录
select * from score where degree>(select degree from score where sno='109' and cno='3-105');
20、查询和学号108、101的同学童年出生的所有学生的sno、sname和sbirthday列
select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
21、查询“张旭”教师任课的学生成绩
    select degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='张旭'));
22、查询选修某课程的同学多于5人的教师姓名
select tname from teacher where tno = (select tno from course where cno=(select cno from score group by cno having count(*) > 5));
23、查询95033和95031班全体学生记录
select * from student where sclass in ('95033', '95031');
24、查询85分以上成绩的课程cno
select cno from score where degree>85;
25、查询出“计算机系”教师所有课程的成绩表
select * from score where cno in(select cno from course where tno in (select tno from teacher where depart='计算机系'));
26、查询计算机系和电子工程系不同职称的教师的tname和prof(职称在别的部门没有出现过)
union求并集
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='计算机系');
27、查询选修编号为3-105,且至少高于选修编号为3-245的同学的信息并按照degree降序排列
(至少表示大于其中任意一个,用any)
select * from score where cno = '3-105' and degree > any(select degree from score where cno = '3-245') order by degree desc;
28、查询选修编号为3-105,且成绩高于选修编号为3-245的同学的信息
(且表示大于所有选项,用all)
select * from  score where cno = '3-105' and degree > all(select degree from score where cno = '3-245’);
29、查询所有教师和同学的name、sex和birthday(别名as,默认会写第一组的名字所以只要改第一组的名字就ok)
select sname as name, ssex as sex, sbirthday as birthdy from student
union
select tname,tsex,tbirthday from teacher;
30、查询所有女教师和女同学的name、sex和birthday
select sname as name, ssex as sex, sbirthday as birthday from student where ssex = '女'
union
select tname, tsex, tbirthday from teacher where tsex = '女';
31、查询成绩比该课程平均成绩低的同学的成绩表
select cno, avg(degree) from score group by cno;//查询一类课程的平均分
select * from score a where degree < (select avg(degree) from score b where a.cno = b.cno); 
32、查询所有任课教师的tname和depart(任课说明在课程表中有课程)
select tname, depart from teacher where tno in (select tno from course);
33、查询至少有两名男生的班号(加条件分组筛选)
select sclass from student where ssex = '男' group by sclass having count(*)>1;
34、查询student表中不姓王的同学记录
select * from student where sname not like '王%';
35、查询student表中每个学生的姓名和年龄
年龄=当前年份-出生年份
select sname, year(now())-year(sbirthday) from student;
36、查询student表中最大最小sbirthday日期值
select sbirthday from student order by sbirthday;
select max(sbirthday) as '最大',min(sbirthday) as '最小' from student;
37、以班号和年龄从大到小排序查询student表中的全部记录
select * from student order by sclass desc, year(now)-year(sbirthday) desc;
select * from student order by sclass desc, sbirthday;
38、查询男教师及其所教的课程
select cname from course where tno in (select tno from teacher where tsex='男');
39、查询最高分同学的sno、cno和degree列
select * 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 sclass = (select sclass from student where sname = '李军');
42、查询所有选修计算机导论课程的男同学的成绩表
select * from score where sno in (select sno from student where ssex = '男') and cno = (select cno from course where cname = '计算机导论');
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(50,59,'E');
查询所有同学的sno、cno和grade列
select sno,cno,grade from score,grade where degree between low and upp;

mysql select查询语句大全相关推荐

  1. [mysql] select查询语句大全指南

    有关mysql其他的命令语句可点击此处获取 本博客使用如下表结构作为例子讲解 create table commoditytype(ct_id int primary key auto_increme ...

  2. mysql的查询语句大全_sql语句(sql数据库查询语句大全)

    sql语句 结构化查询语言(StructuredQueryLanguage)缩写为SQL.结构化查询语言是一种数据库查询和编程语言,用于访问数据以及查询,更新和管理关系数据库系统: 程序功能 创建数据 ...

  3. mysql select查询语句_mysql的select查询语句

    1.简单查询 mysql> select * from students; +------------+----------+------+------+ | id | sname | sex ...

  4. MySQL SELECT查询语句练习1(初级篇)

    设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 以下代码本人均实测,可能部分答案仍有优化空间,欢迎留言指正. 表(一 ...

  5. MySQL SELECT查询语句练习2(中级篇)

    导入现有数据库数据: SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0;-- ---------------------------- -- Table struc ...

  6. MySQL语句系列--查询语句大全(有示例)

    原文网址:MySQL语句系列--查询语句大全(有示例)_IT利刃出鞘的博客-CSDN博客 简介 本文介绍MySQL查询语法(命令)的使用.包括:基本查询.运算条件.排序.分页.分组.联结.组合.子查询 ...

  7. 网上搜集的MySQL查询语句大全集锦(经典珍藏)

    原文地址为: 网上搜集的MySQL查询语句大全集锦(经典珍藏) SQL查询语句大全集锦 MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW ...

  8. 【MySQL】4、Select查询语句

    4.Select查询语句 4.1.select语句 <?php $servername = "localhost"; $username = "username&q ...

  9. mysql 伪表查询语句_MySQL数据库之select查询语句

    select查询语句 语法 select [选项] 列名 [from 表名] [where 条件] [group by 分组] [order by 排序][having 条件] [limit 限制] ...

最新文章

  1. Java stream! Kafka steam!流式处理这么火!它究竟是个啥?
  2. python输出玫瑰花_如何用python画玫瑰花
  3. python方法大全参数是对象_向对象方法Python传递太多参数
  4. 信息系统项目管理06——项目进度管理
  5. 数据结构上机实践第八周项目1- 建立顺序串的算法库
  6. python批量命名文件_用python实现批量重命名文件的代码
  7. VS编译器中QT版本问题不正确解决方法-Please assign a Qt installation in ‘Qt Project Settings‘.
  8. marlab中主成分得分怎么求_线性回归中多重共线性处理——主成分分析法
  9. 指派问题匈牙利解法以及其优化
  10. 试用Windows Server 2008
  11. hd4400 显卡opencore 下的 8个苹果问题解决方法
  12. java工程师职责负责_java工程师岗位职责
  13. php 0x80004005,解决Access出现Microsoft JET Database Engine (0x80004005)未指
  14. mongoDB快速入门
  15. acwing每日一题之贝茜放慢脚步
  16. 大数据分析应用的九大领域
  17. cannon的英文名_卡农的作者是谁啊 此曲的赏析 英文名cannon不是大炮吗
  18. Android开发之获取SIM卡信息和手机号码
  19. 计算机毕业设计ssm流浪动物管理系统q5cx7系统+程序+源码+lw+远程部署
  20. ddos攻击怎么防?防御ddos的几种方法

热门文章

  1. 西子奥的斯电梯服务器使用教程方法_奥的斯电梯服务器按键说明
  2. 数据中心的边缘效应论
  3. DATCOM GUI——DATCOM-Tools使用介绍
  4. Java设计模式——UML类图
  5. sqlserver中文生僻字乱码问题
  6. 【最大流】HDU 4888 Redraw Beautiful Drawings
  7. nginx伪静态(超级简单)
  8. php国外地址生成,生成讯雷地址php代码
  9. acc html播放,有关html Object标签播放flash的AllowScriptAccess与allowNetworking 属性 [转]
  10. 勒索软件_改善市政勒索软件防御能力的3种习惯