本文中共用到了四张表:学生表、课程表、教师表、成绩表
student(s_id,s_name,s_age,s_sex)
s_id : 学⽣编号、s_name: 学⽣姓名、s_age: 出⽣年⽉、s_sex: 学⽣性别
course(c_id,c_name,t_id)
c_id: 课程编号、c_name: 课程名称、t_id: 教师编号
teacher(t_id,t_name)
t_id: 教师编号、t_name: 教师姓名
sc(s_id,c_id,score)
s_id: 学生编号、c_id: 课程编号、score: 分数
本文中我创建了库名为MySQL50的数据库
CREATE DATABASE IF NOT EXISTS MySQL50;
为了避免创建表时创建在错误的数据库,所以先使用该数据库
USE MySQL50;
CREATE TABLE student(
s_id INT PRIMARY KEY AUTO_INCREMENT,
s_name VARCHAR(10),
s_age DATETIME,
s_sex VARCHAR(10)
);
insert into student(s_name,s_age,s_sex) values
( '赵雷' , '1990-01-01' , '男'),
( '钱电' , '1990-12-21' , '男'),
( '孙风' , '1990-04-10' , '男'),
( '李云' , '1990-04-06' , '男'),
( '周梅' , '1991-12-01' , '女'),
( '吴兰' , '1992-03-01' , '女'),
( '郑竹' , '1989-07-01' , '女'),
( '王菊' , '1990-01-20' , '女');
CREATE TABLE course(
c_id INT PRIMARY KEY AUTO_INCREMENT,
c_name VARCHAR(10),
t_id INT(10)
);
INSERT INTO course(c_name,t_id) VALUES
('数学' , 01),
('语文' , 02),
('英语' , 03);
CREATE TABLE teacher(
t_id INT PRIMARY KEY AUTO_INCREMENT,
t_name VARCHAR(10)
);
INSERT INTO teacher(t_name) VALUES
('张三'),
('李四'),
('王五');
CREATE TABLE sc(
s_id INT(10),
c_id INT(10),
score INT(10)
);
INSERT INTO sc(s_id,c_id,score) VALUES
(01 , 01 , 80),
(01 , 02 , 90),
(01 , 03 , 99),
(02 , 01 , 70),
(02 , 02 , 60),
(02 , 03 , 80),
(03 , 01 , 80),
(03 , 02 , 80),
(03 , 03 , 80),
(04 , 01 , 50),
(04 , 02 , 30),
(04 , 03 , 20),
(05 , 01 , 76),
(05 , 02 , 87),
(06 , 01 , 31),
(06 , 03 , 34),
(07 , 02 , 89),
(07 , 03 , 98);
SELECT a1.s_id,a1.s_name,a1.s_sex,b1.s1 AS "课程1成绩",b1.s2 AS "课程2成绩" FROM student a1, (
SELECT a.s_id AS s_id,a.score AS s1,b.score AS s2 FROM sc a,sc b
WHERE a.c_id=1 AND b.c_id=2 AND a.s_id=b.s_id
)b1
WHERE s1>s2 AND a1.s_id=b1.s_id;
select s_id,avg(score) AS "平均分" from sc group by s_id having avg(score)>60;
SELECT s_id,COUNT(*) AS "课程总数",SUM(score) AS "总分"FROM sc GROUP BY s_id;
SELECT COUNT(*)AS "人数" FROM teacher WHERE t_name LIKE "李%";
SELECT a.* FROM student a
JOIN sc b ON a.s_id=b.s_id
JOIN course c ON b.c_id=c.c_id
JOIN teacher d ON c.t_id=d.t_id AND d.t_name="张三";
SELECT c.s_name AS "姓名",c.s_id AS "ID" FROM student AS c,
(SELECT a.s_id AS s_id FROM (SELECT * FROM sc WHERE c_id=1) a ,(SELECT * FROM sc WHERE c_id=2) b
WHERE a.s_id=b.s_id)d
WHERE c.s_id=d.s_id;
SELECT c.s_name AS "姓名",c.s_id AS "ID"FROM student AS c,
(SELECT DISTINCT(a.s_id) AS s_id FROM (SELECT * FROM sc WHERE c_id=1)a,
(SELECT * FROM sc WHERE s_id NOT IN (SELECT s_id FROM sc WHERE c_id=2))b
WHERE a.s_id =b.s_id) d
WHERE c.`s_id`=d.s_id;
SELECT a.s_name AS "姓名",b.s_id AS "ID" FROM student AS a,sc AS b
WHERE a.s_id=b.s_id GROUP BY a.s_id HAVING COUNT(*)<(SELECT COUNT(DISTINCT c_name)AS a FROM course);
SELECT a.s_name AS "姓名",a.s_id AS "ID" FROM student AS a,
(SELECT DISTINCT(s_id) FROM sc WHERE c_id IN(SELECT c_id FROM sc WHERE s_id=1))b WHERE a.s_id=b.s_id;
SELECT s_id,COUNT(*) AS "课程数" FROM sc WHERE c_id IN
(SELECT c_id FROM sc WHERE s_id=1)
GROUP BY s_id HAVING COUNT(*)=(SELECT COUNT(*) FROM sc WHERE s_id=1);
SELECT DISTINCT s_id FROM sc WHERE s_id NOT IN
( SELECT s_id FROM sc WHERE c_id=
(SELECT c_id FROM course WHERE t_id=
(SELECT t_id FROM teacher WHERE t_name="张三") ) );
SELECT s_id,COUNT(*) FROM sc WHERE sc<60 GROUP BY s_id HAVING COUNT(*)>=2;
SELECT s_id,score FROM sc WHERE score<60 AND c_id=1 ORDER BY score DESC;
SELECT a.*,b.avg FROM sc AS a ,
(SELECT s_id,AVG(score) AS AVG FROM sc GROUP BY s_id )b
WHERE a.s_id =b.s_id ORDER BY b.avg DESC;
select c_id,max(score) as "最高分",min(score) as "最低分",avg(score) as "平均分" from sc group by c_id;
SELECT c_id,s_id,score,rank() over (PARTITION BY c_id ORDER BY score DESC) "排名"FROM sc ;
SELECT s_id AS "ID",SUM(score) AS "总分",rank()over(ORDER BY SUM(score) DESC) "排名"
FROM sc GROUP BY s_id ;
SELECT a.c_id,c.t_name,b.c_name,AVG(a.score)AS "平均分" FROM sc a
JOIN course b ON a.c_id=b.c_id
JOIN teacher c ON b.t_id=c.t_id
GROUP BY a.c_id ORDER BY AVG(a.score) DESC;
SELECT c.*,d.c_id,d.score,d.r AS "排名"
FROM student AS c,(SELECT * FROM
(SELECT * ,dense_rank() over(PARTITION BY c_id ORDER BY score DESC)r FROM sc)a
WHERE r BETWEEN 2 AND 3)d
WHERE d.s_id=c.s_id;
SELECT e.c_name,a.c_id,a.c1 AS"100-85分人数",
a.c1/6 AS "100-85分所占百分比",b.c2 AS "85-70分人数",
b.c2/6 AS "85-70百分所占比",c.c3 AS "70-60分人数",
c.c3/6 AS "70-60所占百分比",d.c4 AS "60分以下人数",
d.c4/6 AS "60以下所占百分比"
FROM (SELECT c_id,COUNT(*) c1 FROM sc WHERE score<60 GROUP BY c_id)a
LEFT JOIN (SELECT c_id,COUNT(*) c2 FROM sc WHERE score>= 60 AND score<70 GROUP BY c_id)b ON a.c_id=b.c_id
LEFT JOIN (SELECT c_id,COUNT(*) c3 FROM sc WHERE score>= 70 AND score<85 GROUP BY c_id)c ON a.c_id=c.c_id
LEFT JOIN (SELECT c_id,COUNT(*) c4 FROM sc WHERE score>= 85 GROUP BY c_id)d ON a.c_id=d.c_id
LEFT JOIN course AS e ON a.c_id=e.c_id;
SELECT s_id,AVG(score),rank() over(ORDER BY AVG(score) DESC) "排名"FROM sc GROUP BY s_id ;
SELECT s_id,c_id,score,r AS "排名" FROM
(SELECT *,rank() over(PARTITION BY c_id ORDER BY score DESC)r FROM sc) a WHERE r<4;
SELECT c_id,COUNT(*) AS "人数" FROM sc GROUP BY c_id;
SELECT a.s_id,a.s_name FROM student AS a,sc b WHERE a.s_id=b.s_id GROUP BY b.s_id HAVING COUNT(b.c_id)=2;
SELECT s_sex,COUNT(*) AS "人数" FROM student GROUP BY s_sex;
SELECT * FROM student WHERE s_name LIKE"%风%";
建表时没有录入同名同姓的学生,查询不到数据
SELECT a.s_name,a.s_sex,COUNT(*) FROM student a
JOIN student b ON a.s_name=b.s_name AND a.s_id!=b.s_id
AND a.s_sex=b.s_sex GROUP BY a.s_name,a.s_sex;
SELECT * FROM student WHERE YEAR(s_age)="1990" ;
SELECT c_id,AVG(score) FROM sc GROUP BY c_id ORDER BY AVG(score) DESC;
SELECT a.*,AVG(b.score) AS "平均分" FROM student a,sc b
WHERE a.s_id =b.s_id GROUP BY s_id HAVING AVG(score)>85;
SELECT a.s_name AS "姓名",b.score AS "分数" FROM student a,sc b
WHERE a.s_id=b.s_id AND c_id=(SELECT c_id FROM course WHERE c_name="数学")
AND score<60;
SELECT DISTINCT a.s_id,e.s_name AS "姓名",b.score AS "语文",
c.score AS "学",d.score AS "英语"
FROM sc a LEFT JOIN sc b ON a.s_id=b.s_id AND b.c_id=1
LEFT JOIN sc c ON a.s_id=c.s_id AND c.c_id=2
LEFT JOIN sc d ON a.s_id=d.s_id AND d.c_id=3
LEFT JOIN student e ON a.s_id=e.s_id;
SELECT a.s_name,b.c_name,c.score FROM sc c
JOIN course b ON c.c_id=b.c_id AND c.score>70
JOIN student a ON a.s_id=c.s_id ;
SELECT a.s_name,b.c_name,c.score FROM sc c
JOIN course b ON c.c_id=b.c_id AND c.score<60
JOIN student a ON a.s_id=c.s_id ;
SELECT a.s_id,a.s_name FROM student a,sc b WHERE a.s_id=b.s_id AND b.score>79 AND b.c_id=1;
SELECT a.c_name AS "课程名",COUNT(*) AS "人数" FROM course a,sc b WHERE a.c_id=b.c_id GROUP BY b.c_id;
SELECT d.*,a.score AS "成绩" FROM sc a
JOIN course b ON a.c_id=b.c_id
JOIN teacher c ON b.t_id=c.t_id AND c.t_name="张三"
JOIN student d ON a.s_id=d.s_id ORDER BY score DESC LIMIT 1;
SELECT DISTINCT (a.s_id),a.c_id,a.score FROM sc a
JOIN sc b ON a.s_id=b.s_id AND a.c_id!=b.c_id AND a.score=b.score;
SELECT c_id AS "课程编号",s_id AS "学号",score AS "成绩",
a AS "排名" FROM (SELECT c_id,s_id,score,dense_rank() over(PARTITION BY c_id ORDER BY score DESC)a
FROM sc)b WHERE a<3;
SELECT b.s_name,b.s_id,COUNT(*) AS "选修课程门数"
FROM sc a,student b WHERE a.s_id=b.s_id GROUP BY a.s_id
HAVING COUNT(a.c_id)>=2;
SELECT b.s_name,b.s_id FROM sc a,student b
WHERE a.s_id=b.s_id GROUP BY a.s_id
HAVING COUNT(*)=(SELECT COUNT(*) FROM course);
SELECT s_name,s_id,TIMESTAMPDIFF(YEAR, s_age, CURDATE()) AS "年龄" FROM student;
SELECT * FROM student WHERE WEEKOFYEAR(s_age) = WEEKOFYEAR(CURDATE());
SELECT * FROM student WHERE MONTH(s_age) = MONTH(CURDATE());
SELECT s_id,s_name,s_sex,TIMESTAMPDIFF(YEAR, s_age, CURDATE())
AS "年龄" FROM student WHERE s_sex="男"
AND SUBSTR(s_age,6,7)=12;