sql 作业 7

This commit is contained in:
flykhan 2023-08-24 23:05:08 +08:00
parent e11f684219
commit 3b818d11bd
1 changed files with 94 additions and 0 deletions

94
day9/homework/h7.sql Normal file
View File

@ -0,0 +1,94 @@
/*
stu.sql脚本中的数据表完成下列操作
1
2
3
4
5 33
6
770~80
stu.sql文件 stu.sql文件后 studb2, source
student
teacher
course
sc
floor(datediff(now(), age)/365) as age
*/
-- 删除已存在同名数据库 studb2
drop database if exists studb2;
-- 新建数据库 studb2
create database studb2;
-- 进入数据库 studb2
use studb2;
-- 导入 stu.sql 数据到数据库 stu
source stu.sql;
-- 显示导入结果
show tables;
-- 查询学生选课表中的全部数据
SELECT *
FROM course;
-- 查询全体学生的姓名、学号和所在系
SELECT
student.name AS '姓名',
student.sid AS '学号',
xb.name AS '所在系'
FROM student
JOIN xb
ON student.xid = xb.xid;
-- 查询全体学生的姓名及其出生年份
SELECT
name AS '学生姓名',
YEAR(age) AS '出生年份'
FROM student;
-- 查询计算机系全体学生的姓名
SELECT
xb.name AS '所在系',
student.name AS '姓名'
FROM xb
JOIN student
ON xb.xid = student.xid
WHERE xb.name = '计算机系';
-- 查询年龄在33岁以下的学生的姓名及年龄
SELECT
name AS '姓名',
FLOOR(DATEDIFF(NOW(), age)/365) AS '年龄'
FROM student
WHERE FLOOR(DATEDIFF(NOW(), age)/365) < 33;
-- 查询考试成绩有不及格的学生的学号 DISTINCT 用于结果行去重
SELECT DISTINCT
student.sid AS '学号'
FROM student
JOIN sc
ON student.sid = sc.sid
WHERE sc.score < 60;
-- 查询成绩在70-80分之间的学生包括学号课程号和成绩
SELECT
student.sid AS '学号',
sc.cid AS '课程号',
sc.score AS '成绩'
FROM student
JOIN sc
ON student.sid = sc.sid
-- WHERE sc.score BETWEEN 70 AND 80;
WHERE sc.score >= 70 and sc.score <= 80;