# mysql 练习
#### Student表的定义
| 字段名 | 字段描述 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 |
| ---------- | -------- | ----------- | ---- | ---- | ---- | ---- | ---- |
| id | 学号 | INT(10) | 是 | 否 | 是 | 是 | 是 |
| name | 姓名 | VARCHAR(20) | 否 | 否 | 是 | 否 | 否 |
| sex | 性别 | VARCHAR(4) | 否 | 否 | 否 | 否 | 否 |
| birth | 出生年份 | YEAR | 否 | 否 | 否 | 否 | 否 |
| department | 院系 | VARCHAR(20) | 否 | 否 | 是 | 否 | 否 |
| address | 家庭住址 | VARCHAR(50) | 否 | 否 | 否 | 否 | 否 |
------
#### **Score表的定义**
| 字段名 | 字段描述 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 |
| ------ | -------- | ----------- | ---- | ---- | ---- | ---- | ---- |
| id | 编号 | INT(10) | 是 | 否 | 是 | 是 | 是 |
| stu_id | 学号 | INT(10) | 否 | 否 | 是 | 否 | 否 |
| c_name | 课程名 | VARCHAR(20) | 否 | 否 | 否 | 否 | 否 |
| grade | 分数 | INT(10) | 否 | 否 | 否 | 否 | 否 |
------
#### **系统初始化了以下数据**
- **向student表插入记录的INSERT语句如下**:
```sql
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
```
- **向score表插入记录的INSERT语句如下**:
```sql
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
```
**1.创建student和score表语句**
```sql
-- 创建student表
CREATE TABLE student (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
sex varchar(255) DEFAULT NULL,
birth year(4) DEFAULT NULL,
department varchar(255) NOT NULL,
address varchar(255) DEFAULT NULL
);
-- 创建score表
CREATE TABLE score (
id int(11) AUTO_INCREMENT PRIMARY KEY,
stu_id int(11) NOT NULL,
c_name varchar(255) DEFAULT NULL,
grade int(10) DEFAULT NULL
);
```
**2.查询student表的所有记录**
```sql
SELECT *
FROM student;
```
**3.查询student表的第2条到4条记录**
```sql
SELECT *
FROM STUDENT
LIMIT 1,3;
```
**4.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息**
```sql
SELECT
id AS '学号',
name AS '姓名',
department AS '院系'
FROM student;
```
**5.从student表中查询计算机系和英语系的学生的信息(用 IN 关键字)**
```sql
SELECT *
FROM student
WHERE department IN ('计算机系','英语系');
```
**6.从student表中查询年龄18~22岁的学生信息(用 BETWEEN AND)**
```sql
SELECT *
FROM student
WHERE birth BETWEEN (YEAR(NOW())-22) AND (YEAR(NOW())-18);
SELECT YEAR(NOW()) 查询当前年份
SELECT MONTH(NOW()) 查询当前月份
SELECT DAY(NOW()) 查询当前日
SELECT DATE(NOW()) 查询当前日期
SELECT DATE_FORMAT(NOW(),'%Y') 查询当前年份
```
**7.从student表中查询每个院系有多少人**
```sql
SELECT
department AS '院系',
COUNT(*) AS '人数'
FROM student
GROUP BY department;
```
**8.从score表中查询每个科目的最高分**
```sql
SELECT
c_name AS '科目',
MAX(grade) AS '最高分'
FROM score
GROUP BY c_name;
```
**9.查询李四的考试科目(c_name)和考试成绩(grade)**
```sql
SELECT
st.name AS '姓名',
sc.c_name AS '考试科目',
sc.grade AS '考试成绩'
FROM score sc
LEFT JOIN student st ON st.id = sc.stu_id
WHERE st.name = '李四';
```
**10.用连接的方式查询所有学生的信息和考试信息**
```sql
SELECT *
FROM student st
LEFT JOIN score sc ON st.id = sc.stu_id;
```
**11.计算每个学生的总成绩**
```sql
SELECT
st.name AS '姓名',
SUM(grade) AS '总成绩'
FROM student st
LEFT JOIN score sc ON st.id = sc.stu_id
GROUP BY st.name;
```
**12.计算每个考试科目的平均成绩**
```sql
SELECT
c_name AS '考试科目',
AVG(grade) AS '平均成绩'
FROM score
GROUP BY c_name;
```
**13.查询计算机成绩低于95的学生信息**
```sql
-- 方法一:
SELECT st.*
FROM student st
LEFT JOIN score sc ON sc.stu_id = st.id
WHERE sc.c_name = '计算机' AND sc.grade < 95;
-- 方法二:
SELECT *
FROM student
WHERE id IN
(SELECT stu_id FROM score
WHERE c_name = '计算机' AND grade < 95);
```
**14.查询同时参加计算机和英语考试的学生的信息**
```sql
-- 方法一:
SELECT st.*
FROM student st
LEFT JOIN score sc ON sc.stu_id = st.id
WHERE c_name = '计算机' OR c_name = '英语'
GROUP BY stu_id
HAVING COUNT(*) = 2;
-- 方法二:
SELECT *
FROM student
WHERE id IN
(SELECT stu_id
FROM score
WHERE c_name = '计算机' OR c_name = '英语'
GROUP BY stu_id
HAVING COUNT(*) = 2);
-- 方法三:
SELECT *
FROM student
WHERE id = ANY
( SELECT stu_id FROM score
WHERE stu_id IN (
SELECT stu_id
FROM score
WHERE c_name = '计算机' )AND c_name = '英语');
-- 方法四:
SELECT a.*
FROM student a, score b, score c
WHERE a.id = b.stu_id AND b.c_name = '计算机'
AND a.id = c.stu_id AND c.c_name = '英语';
```
**15.将计算机考试成绩按从高到低进行排序**
```sql
SELECT
c_name AS '课程名',
grade AS '分数'
FROM score
WHERE c_name = '计算机'
ORDER BY grade DESC;
```
**16.从student表和score表中查询出学生的学号,然后合并查询结果**
```sql
SELECT id FROM student
UNION
SELECT stu_id FROM score;
```
**17.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩**
```sql
SELECT
st.name AS '姓名',
st.department AS '院系',
sc.c_name AS '考试科目',
sc.grade AS '成绩'
FROM score sc
LEFT JOIN student st ON sc.stu_id = st.id
WHERE st.name LIKE '张%' OR st.name LIKE '王%';
```
**18.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩**
```sql
SELECT
s.name AS '姓名',
YEAR(NOW())-s.birth AS '年龄',
c.c_name AS '考试科目',
c.grade AS '成绩',
s.address AS '家庭住址'
FROM score AS c
LEFT JOIN student AS s ON c.stu_id = s.id
WHERE s.address LIKE '湖南%';
```