- 查询被选修过的课程编号(去除重复)
查询分配了学生的班级编号(去除重复)
mysql> select distinct 字段名 from 表名;
mysql> select distinct cno from t_score;
+------+
| cno |
+------+
| C01 |
| C02 |
| C03 |
| C04 |
+------+
4 rows in set
mysql> select distinct gno from t_student;
+---------+
| gno |
+---------+
| NULL |
| JX1433 |
| JX1434 |
| RK1431 |
| RK1432 |
| YD1435 |
| YD1436 |
+---------+
7 rows in set
- 查询学分最多前两门课程信息
查询单科成绩最高前三名学生学号
mysql> select * form 表名 order by 字段名 desc limit 数字;
mysql> select * from t_course order by cpoint desc limit 2;
+------+------------+-------+------------------------+
| cno | cname | cpoint | remark |
+------+------------+-------+------------------------+
| C03 | 数据库原理 | 5 | 学习数据库相关知识 |
| C02 | 计算机科学 | 4 | 学习计算机相关基础知识 |
+------+------------+-------+------------------------+
2 rows in set
mysql> select * from t_score order by grade desc limit 3;
+------+------------+-------+
| cno | sno | grade |
+------+------------+-------+
| C04 | 1618053138 | 98 |
| C01 | 1625153220 | 98 |
| C03 | 1618053137 | 98 |
+------+------------+-------+
3 rows in set
- 以某格式查询显示信息
mysql> select 列名1 as '新的列名1',...,列名n as '新的列名n'from 表名;
mysql> select cno as '班级编号',cname as '班级名称'from t_course;
+----------+---------------+
| 班级编号 | 班级名称 |
+----------+---------------+
| C01 | 计算机英语 |
| C02 | 计算机科学 |
| C03 | 数据库原理 |
| C04 | 大学人文 |
| C05 | JAVA面向对象 |
| C06 | HTML5编程 |
+----------+---------------+
6 rows in set
mysql> select sno as '学号',sname as '姓名',birthday as '出生日期'from t_student;
mysql> select sno as '学号',cno as '课程编号',grade as '分数'from t_score;
4.查询班级编号为JX1433的班级名称和导师姓名
MySQL> select 查询内容 from 表名 where 查询列名 = '某要求';
mysql> select gname,gmentor from t_group where gno = 'JX1433';
+------------+---------+
| gname | gmentor |
+------------+---------+
| 计信1433班 | 张学友 |
+------------+---------+
1 row in set
查询班级编号为JX1433的所有学生信息
mysql> select * from t_group where gno = 'JX1433';
+--------+------------+---------+
| gno | gname | gmentor |
+--------+------------+---------+
| JX1433 | 计信1433班 | 张学友 |
+--------+------------+---------+
1 row in set
查询学分大于等于3学分的所有课程信息
mysql> select * from t_course where cpoint>=3;
+------+---------------+-------+------------------------+
| cno | cname | cpoint | remark |
+------+---------------+-------+------------------------+
| C01 | 计算机英语 | 3 | 学习IT相关专业英语 |
| C02 | 计算机科学 | 4 | 学习计算机相关基础知识 |
| C03 | 数据库原理 | 5 | 学习数据库相关知识 |
| C05 | JAVA面向对象 | 3 | 学习最流行的编程语言 |
| C06 | HTML5编程 | 3 | 学习HTML5的相互知识 |
+------+---------------+-------+------------------------+
5 rows in set
查询成绩不及格的所有学号和课程编号
mysql> select sno,cno from t_score where grade<60;
5.## 满足一个或多个条件,注意运算符(or,and,非not)的使用
mysql> select 查询内容 from 表名 where 查询条件;
查询RK1431班的女学生信息
查询成龙和刘德华两个导师所带的班级信息
查询学分为3的课程信息
mysql> select * from t_student where gender = '女' and gno = 'RK1431';
mysql> select * from t_group where gmentor = '成龙' or gmentor ='刘德华';
+---------+------------+---------+
| gno | gname | gmentor |
+---------+------------+---------+
| JX1434 | 计信1434班 | 成龙 |
| RK144 rows in set
mysql> select * from t_course where cpoint=3;
+------+---------------+-------+----------------------+
| cno | cname | cpoint | remark |
+------+---------------+-------+----------------------+
| C01 | 计算机英语 | 3 | 学习IT相关专业英语 |
| C05 | JAVA面向对象 | 3 | 学习最流行的编程语言 |
| C06 | HTML5编程 | 3 | 学习HTML5的相互知识 |
+------+---------------+-------+----------------------+
3 rows in set
查询学分不为3的课程信息(用NOT)
mysql> select * from t_course where not cpoint = 3;
+------+------------+-------+------------------------+
| cno | cname | cpoint | remark |
+------+------------+-------+------------------------+
| C02 | 计算机科学 | 4 | 学习计算机相关基础知识 |
| C03 | 数据库原理 | 5 | 学习数据库相关知识 |
| C04 | 大学人文 | 2 | 学习传统文化知识 |
+------+------------+-------+------------------------+
3 rows in set
mysql> select * from t_course where cpoint != 3;
+------+------------+-------+------------------------+
| cno | cname | cpoint | remark |
+------+------------+-------+------------------------+
| C02 | 计算机科学 | 4 | 学习计算机相关基础知识 |
| C03 | 数据库原理 | 5 | 学习数据库相关知识 |
| C04 | 大学人文 | 2 | 学习传统文化知识 |
+------+------------+-------+------------------------+
3 rows in set
- 查询没有分配班级的学生信息
查询没有指定导师的班级信息
查询没有填写性别的学生信息,未填写的性别以’不详‘显示
mysql> select * from t_student where gno is null;
+------------+--------+--------+------------+------+
| sno | sname | gender | birthday | gno |
+------------+--------+--------+------------+------+
| 1625123811 | 雷程钧 | 男 | 1996-02-27 | NULL |
| 1625133437 | 宋凯 | 男 | 1997-11-27 | NULL |
| 1625153221 | 胡云飞 | 男 | 1997-11-19 | NULL |
| 1625153236 | 刘鹏 | 男 | 1997-02-07 | NULL |
| 1625163107 | 谢京霖 | 男 | 1998-10-01 | NULL |
+------------+--------+--------+------------+------+
5 rows in set
mysql> select * from t_group where gmentor is null;
Empty set
mysql> select *,ifnull(gender,'不详') from t_student;
+------------+----------+--------+------------+---------+------------------+
| sno | sname | gender | birthday | gno | ifnull(gender,'不详') |
+------------+----------+--------+------------+---------+------------------+
mysql> select *,ifnull(gender,'不详') as gender2 from t_student;
+------------+----------+--------+------------+---------+---------+
| sno | sname | gender | birthday | gno | gender2 |
+------------+----------+--------+------------+---------+---------+
查询学分为2、4、5的课程信息
查询成绩在80到90之间的学生学号和课程编号
查询出生日期不是1996、1997、1998这三年国庆出生的学生信息
查询出生日期不是1996年出生的学生信息
mysql> select * from t_course where cpoint in('2','4','5');
+------+------------+-------+------------------------+
| cno | cname | cpoint | remark |
+------+------------+-------+------------------------+
| C02 | 计算机科学 | 4 | 学习计算机相关基础知识 |
| C03 | 数据库原理 | 5 | 学习数据库相关知识 |
| C04 | 大学人文 | 2 | 学习传统文化知识 |
+------+------------+-------+------------------------+
3 rows in set
mysql> select cno,sno from t_score where grade between '80' and '90';
+------+------------+
| cno | sno |
+------+------------+
| C01 | 1618053138 |
| C01 | 1625153214 |
| C01 | 1625153221 |
| C02 | 1618053139 |
| C02 | 1618053146 |
| C02 | 1618053147 |
| C02 | 1625123747 |
| C02 | 1625153221 |
| C03 | 1618053139 |
| C03 | 1618053146 |
| C03 | 1625123748 |
| C04 | 1618053142 |
| C04 | 1625133432 |
+------+------------+
13 rows in set
mysql> select * from t_student where birthday not between '1996-01-01' and '1996-12-31';
mysql> select * from t_student where birthday not in('1996-10-01','1997-10-01','1998-10-01');