① 本文整理了经典的 50 道 SQL 题目,文本分为建库建表、插入数据以及 SQL 50 题这三个部分。
② 这些题目许多博主也整理过,但本人不太了解这些题目具体的出处。第一次了解这些题目是本科期间老师出的题目。如果有网友知道这些题目的最原始出处,可以在评论评论区中告知。
③ 本文所使用的 MySQL 版本为 5.5,虽然版本有一点旧,但是对 SQL 知识点的复习没有太大的影响(除了一些旧版没有的函数)。
④ 由于本文旨在对 SQL 基础知识进行复习,并且所涉及的数据量也十分的小,所以在编写 SQL 语句时,并未过多考虑 SQL 优化的方面。如果读者有其它的解法或者发现错误之处,可在评论区留言,笔者在看到后会及时更新!
(1)建库:创建一个名为 sqlpractice 的数据库。
(2)建表:建立 student、course、teacher 和 score 这 4 张表。它们的字段以及之间的关系如下图所示。
(3)建库建表的完整 SQL 语句如下所示。
# 建库
create database sqlpractice;
use sqlpractice;
# 建立 Student 学生表
CREATE TABLE Student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL,
s_birth VARCHAR(20) NOT NULL,
s_sex VARCHAR(10) NOT NULL,
PRIMARY KEY(s_id) # 主键
);
# 建立 Course 课程表
CREATE TABLE Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id) # 主键
);
# 建立 Teacher 教师表
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id) # 主键
);
# 建立 Score 分数表
CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id, c_id) # 联合主键
);
# 添加外键
# 语法:ALTER TABLE 从表 ADD FOREIGN KEY(外键字段) REFERENCES 主表(主键字段)
ALTER TABLE Course ADD FOREIGN KEY(t_id) REFERENCES Teacher(t_id)
ALTER TABLE Score ADD FOREIGN KEY(s_id) REFERENCES Student(s_id)
ALTER TABLE Score ADD FOREIGN KEY(c_id) REFERENCES Course(c_id)
(1)向上面创建的 4 张表中插入测试数据的 SQL 语句如下所示(需要注意表之间的关系,以免插入数据失败)。
# 分别向四张表中插入数据
INSERT INTO Student VALUES('01', '赵雷', '1990-01-01', '男');
INSERT INTO Student VALUES('02', '钱电', '1990-12-21', '男');
INSERT INTO Student VALUES('03', '孙风', '1990-05-20', '男');
INSERT INTO Student VALUES('04', '李云', '1990-08-06', '男');
INSERT INTO Student VALUES('05', '周梅', '1991-12-01', '女');
INSERT INTO Student VALUES('06', '吴兰', '1992-03-01', '女');
INSERT INTO Student VALUES('07', '郑竹', '1989-07-01', '女');
INSERT INTO Student VALUES('08', '王菊', '1990-01-20', '女');
INSERT INTO Teacher VALUES('01', '张三');
INSERT INTO Teacher VALUES('02', '李四');
INSERT INTO Teacher VALUES('03', '王五');
INSERT INTO Course VALUES('01', '语文', '02');
INSERT INTO Course VALUES('02', '数学', '01');
INSERT INTO Course VALUES('03', '英语', '03');
INSERT INTO Score VALUES('01', '01', 80);
INSERT INTO Score VALUES('01', '02', 90);
INSERT INTO Score VALUES('01', '03', 99);
INSERT INTO Score VALUES('02', '01', 70);
INSERT INTO Score VALUES('02', '02', 60);
INSERT INTO Score VALUES('02', '03', 80);
INSERT INTO Score VALUES('03', '01', 80);
INSERT INTO Score VALUES('03', '02', 80);
INSERT INTO Score VALUES('03', '03', 80);
INSERT INTO Score VALUES('04', '01', 50);
INSERT INTO Score VALUES('04', '02', 30);
INSERT INTO Score VALUES('04', '03', 20);
INSERT INTO Score VALUES('05', '01', 76);
INSERT INTO Score VALUES('05', '02', 87);
INSERT INTO Score VALUES('06', '01', 31);
INSERT INTO Score VALUES('06', '03', 34);
INSERT INTO Score VALUES('07', '02', 89);
INSERT INTO Score VALUES('07', '03', 98);
(2)检验插入数据是否成功
SELECT * FROM Student;
SELECT * FROM Course;
SELECT * FROM Teacher;
SELECT * FROM Score;
Course 表
Teacher 表
Score 表
# 本题需要比较"01"课程比"02"课程的成绩,故在 where 中将 score 表中的字段 s_score 使用 2 次(即分别对应"01"课程的成绩和"02"课程的成绩)
# 所以可以使用为 s_score 表取别名的方式来多次使用 score 表中的字段
SELECT
student.*,
score1.s_score
FROM
student,
score AS score1,
score AS score2
WHERE
student.s_id = score1.s_id
AND score1.s_id = score2.s_id # student, score1, score2 表连接的条件是它们的 s_id 均相等
AND score1.c_id = '01'
AND score2.c_id = '02'
AND score1.s_score > score2.s_score;
SELECT
student.*,
score1.s_score
FROM
student,
score AS score1,
score AS score2
WHERE
student.s_id = score1.s_id
AND score1.s_id = score2.s_id # student, score1, score2 表连接的条件是它们的 s_id 均相等
AND score1.c_id = '01'
AND score2.c_id = '02'
AND score1.s_score < score2.s_score;
# 1.创建临时表 ss
EXPLAIN SELECT
student.s_id,
student.s_name,
ss.avg_score
FROM
student,
(SELECT s_id, AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS ss
WHERE
student.s_id = ss.s_id
AND ss.avg_score >= 60;
# 2.先进行内连接,然后再分组
SELECT
student.s_id,
s_name,
round(AVG(score.s_score), 2) as avg_score
FROM
student
INNER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
s_name
HAVING
AVG(score.s_score) >= 60
# isnull(exper) 判断 exper 是否为空,是则返回 1,否则返回 0
# ifnull(exper1, exper2) 判断 exper1 是否为空,是则用 exper2 代替
# nullif(exper1, exper2) 如果 expr1 = expr2 成立,那么返回值为 NULL,否则返回值为 expr1。
SELECT
student.s_id,
s_name,
round(AVG(score.s_score), 2) as avg_score
FROM
student
LEFT OUTER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
s_name
HAVING
AVG(IFNULL(score.s_score,0)) < 60
SELECT
student.s_id,
student.s_name,
COUNT(DISTINCT c_id) AS totalCourses,
SUM(s_score) AS totalScores
FROM
student
# 由于要查询所有的学生,故无论其是否有课程信息都要查询,所以使用 LEFT OUTER JOIN
LEFT OUTER JOIN score ON student.s_id = score.s_id
GROUP BY
student.s_id,
student.s_name;
# 1.模糊查询
SELECT
COUNT(*)
FROM
teacher
WHERE
t_name LIKE '李%'
# 2.正则表达式查询,字符 '^' 匹配以特定字符或者字符串开头的文本
SELECT
count(*)
FROM
teacher
WHERE
t_name REGEXP '^李'
# 1.使用多表连接(score, course, teacher)找到上张三老师课的同学的 s_id,然后再根据 s_id 从 student 表中查询同学信息
SELECT
student.*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
score,
course,
teacher
WHERE
teacher.t_name = '张三'
AND teacher.t_id = course.t_id
AND course.c_id = score.c_id
)
# 2.多层嵌套子查询(当数据量较大时,一般不推荐使用子查询)
# 在 student 表中根据上过张三老师教的课的学生 s_id 来查询他们的信息
SELECT
student.*
FROM
student
WHERE
student.s_id IN (
# 在 score 表中根据张三老师教的课程 c_id 来查找上这些课的学生 s_id
SELECT DISTINCT
s_id
FROM
score
WHERE
score.c_id IN (
# 在 course 表中根据张三老师的 t_id 查询他所教的课程 c_id
SELECT
c_id
FROM
course
WHERE
course.t_id = (
# 在 teacher 表中查询张三老师的 t_id
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
)
SELECT
student.*
FROM
student
WHERE
student.s_id NOT IN (
SELECT DISTINCT
s_id
FROM
score
WHERE
score.c_id IN (
SELECT
c_id
FROM
course
WHERE
course.t_id = (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
)
SELECT
student.*
FROM
student
WHERE
student.s_id IN (
SELECT
s1.s_id
FROM
score AS s1,
score AS s2
WHERE
s1.s_id = s2.s_id
AND s1.c_id = '01'
AND s2.c_id = '02'
)
SELECT
stu.s_id,
stu.s_name,
stu.s_birth,
stu.s_sex
FROM
student AS stu
JOIN score AS sc ON stu.s_id = sc.s_id
JOIN course AS co ON co.c_id = sc.c_id
WHERE
co.c_id = '01'
AND stu.s_id NOT IN (
# 查询学过编号为 "02" 的课程的同学 id
SELECT
stu.s_id
FROM
student AS stu
JOIN score AS sc ON stu.s_id = sc.s_id
JOIN course AS co ON co.c_id = sc.c_id
WHERE
co.c_id = '02'
)
# 下面的课程数量 3 也可以用 (SELECT count(*) FROM course) 来代替
SELECT
*
FROM
student
WHERE
s_id IN (SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) < 3)
# 不包括学号为 '01' 学生自己
SELECT
*
FROM
student
WHERE
s_id IN (
SELECT DISTINCT s_id FROM score
WHERE c_id IN (SELECT c_id FROM score WHERE s_id = '01')
AND s_id != '01'
)
SELECT
*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
score
WHERE
# 保证学习的课程相同
c_id IN (SELECT DISTINCT c_id FROM score WHERE s_id = '01')
AND s_id != '01'
GROUP BY
s_id
HAVING
# 保证学习的课程数量相同
count(c_id) = (select count(*) from score where s_id = '01')
)
SELECT
s_name
FROM
student
WHERE
s_id NOT IN (
# 查询学习过"张三"老师讲授的任一门课程的学生 id
SELECT
s_id
FROM
score
WHERE
c_id IN (
# 查询由姓名为张三的老师所讲授的课程 id
SELECT
c_id
FROM
course
WHERE
t_id IN (SELECT t_id FROM teacher WHERE t_name = '张三')
)
)
SELECT
stu.s_id,
stu.s_name,
tmp_t.avg_score
FROM
student AS stu
INNER JOIN
(
SELECT
s_id,
round(avg(s_score), 2) AS avg_score
FROM
score
WHERE
s_score < 60
GROUP BY
s_id
HAVING
count(s_score) >= 2
) AS tmp_t
ON
stu.s_id = tmp_t.s_id
SELECT
stu.*
FROM
student AS stu
INNER JOIN score ON stu.s_id = score.s_id
WHERE
c_id = '01'
AND
s_score < 60
ORDER BY
s_score DESC
SELECT
s_id,
max(CASE c_id WHEN '01' THEN s_score ELSE 0 END) AS '01',
max(CASE c_id WHEN '02' THEN s_score ELSE 0 END) AS '02',
max(CASE c_id WHEN '03' THEN s_score ELSE 0 END) AS '03',
avg(s_score) AS avg_score
FROM
score
GROUP BY
s_id
ORDER BY
avg_score DESC
SELECT
sc.c_id AS "课程ID",
c.c_name AS '课程名称',
MAX(sc.s_score) AS "最高分",
MIN(sc.s_score) AS '最低分',
AVG(sc.s_score) AS '平均分',
SUM(IF (sc.s_score BETWEEN 60 AND 70, 1, 0)) / COUNT(*) as '及格率',
SUM(IF (sc.s_score BETWEEN 71 AND 80, 1, 0)) / COUNT(*) as '中等率',
SUM(IF (sc.s_score BETWEEN 81 AND 90, 1, 0)) / COUNT(*) as '优良率',
SUM(IF (sc.s_score >= 91, 1, 0)) / COUNT(*) as '优秀率'
FROM
score AS sc
JOIN course AS c ON sc.c_id = c.c_id
GROUP BY
sc.c_id
SELECT
sc1.c_id,
sc1.s_id,
sc1.s_score,
count(sc2.s_score) + 1 AS rank
FROM
score AS sc1 LEFT JOIN score AS sc2
ON sc1.s_score < sc2.s_score
AND sc1.c_id = sc2.c_id
GROUP BY
sc1.c_id,
sc1.s_id,
sc1.s_score
ORDER BY
sc1.c_id,
rank
SELECT
stu.s_id,
stu.s_name,
total_score,
(
SELECT COUNT(DISTINCT total_score)
FROM (SELECT SUM(s_score) AS total_score FROM score GROUP BY s_id) AS sub
WHERE total_score >= tmp.total_score
) AS rank
FROM
student as stu
INNER JOIN (
SELECT
s_id,
SUM(s_score) AS total_score
FROM
score
GROUP BY
s_id
) AS tmp ON stu.s_id = tmp.s_id
ORDER BY
total_score DESC;
SELECT
teacher.t_id,
t_name,
round(avg(s_score), 2) AS avg_score
FROM
teacher,
course,
score
WHERE
teacher.t_id = course.t_id
AND course.c_id = score.c_id
GROUP BY
teacher.t_id,
t_name,
score.c_id
ORDER BY
avg(score.s_score) DESC
# 1.分别对每门课程进行查询,然后再合并查询结果,但是如果课程太多,该方法就不太合适
SELECT
t1.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "01"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t1
UNION ALL
SELECT
t2.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "02"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t2
UNION ALL
SELECT
t3.*
FROM
(
SELECT
st.*,
c.c_id,
c.c_name,
sc.s_score
FROM
student st
LEFT JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id = sc.c_id
AND c.c_id = "03"
ORDER BY
sc.s_score DESC
LIMIT 1,
2
) as t3
# 2.一次性查询,需要注意的是 row_number() 在 MySQL 8.0 中才支持
SELECT
c_id,
student.*,
s_score
FROM
student
INNER JOIN (
SELECT
s_id,
s_score,
c_id,
row_number() over (PARTITION BY c_id ORDER BY s_score DESC) AS rank
FROM
score
) AS tmp_t ON tmp_t.s_id = student.s_id
WHERE
tmp_t.rank IN (2, 3)
SELECT
score.c_id,
course.c_name,
sum(CASE WHEN s_score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) AS '[0-60]人数',
sum(CASE WHEN s_score BETWEEN 61 AND 70 THEN 1 ELSE 0 END) AS '[61-70]人数',
sum(CASE WHEN s_score BETWEEN 71 AND 85 THEN 1 ELSE 0 END) AS '[71-85]人数',
sum(CASE WHEN s_score BETWEEN 86 AND 100 THEN 1 ELSE 0 END) AS '[86-100]人数',
round(sum(CASE WHEN s_score BETWEEN 0 AND 60 THEN 1 ELSE 0 END) / count(*), 2) AS '[0-60]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 61 AND 70 THEN 1 ELSE 0 END) / count(*), 2) AS '[61-70]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 71 AND 85 THEN 1 ELSE 0 END) / count(*), 2) AS '[71-85]人数所占百分比',
round(sum(CASE WHEN s_score BETWEEN 86 AND 100 THEN 1 ELSE 0 END) / count(*), 2) AS '[86-100]人数所占百分比'
FROM
score LEFT JOIN course
ON score.c_id = course.c_id
GROUP BY
score.c_id,
course.c_name
# 在 MySQL 8 中可以使用 rank 函数来实现排名
SELECT
stu.s_id,
stu.s_name,
round(avg(sc.s_score), 2) AS average_score,
(
SELECT COUNT(DISTINCT avg_score)
FROM (SELECT AVG(s_score) AS avg_score FROM score GROUP BY s_id) AS sub
WHERE avg_score >= AVG(sc.s_score)
) AS rank
FROM
student as stu
INNER JOIN score as sc ON stu.s_id = sc.s_id
GROUP BY
stu.s_id,
stu.s_name
ORDER BY
average_score DESC;
# 1.分别对每科进行查询,然后再合并查询结果,但是如果课程太多,该方法就不太合适
(SELECT c_id, s_score FROM score WHERE c_id = '01' ORDER BY s_score DESC LIMIT 3) UNION ALL
(SELECT c_id, s_score FROM score WHERE c_id = '02' ORDER BY s_score DESC LIMIT 3) UNION ALL
(SELECT c_id, s_score FROM score WHERE c_id = '03' ORDER BY s_score DESC LIMIT 3)
# 2.一次性查询出结果
SELECT DISTINCT
tmp_t.c_id,
tmp_t.s_score
FROM
(
SELECT DISTINCT
student.*,
sc1.c_id,
sc1.s_score,
count(DISTINCT sc2.s_score) + 1 AS rank
FROM
score AS sc1
LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id
AND sc1.s_score < sc2.s_score
LEFT JOIN student ON sc1.s_id = student.s_id
GROUP BY
sc1.c_id,
sc1.s_id
ORDER BY
sc1.c_id,
sc1.s_score DESC
) AS tmp_t
WHERE
tmp_t.rank BETWEEN 1 AND 3
SELECT
c_id,
count( s_id ) AS '选修该门课程的学生数'
FROM
score
GROUP BY
c_id
SELECT
student.s_id,
student.s_name
FROM
student,
score
WHERE
student.s_id = score.s_id
GROUP BY
s_id
HAVING
count(c_id) = 2
SELECT
sum(CASE WHEN s_sex = '男' THEN 1 ELSE NULL END) AS '男生人数',
sum(CASE WHEN s_sex = '女' THEN 1 ELSE NULL END) AS '女生人数'
FROM
student
# 1.使用模糊匹配
SELECT
*
FROM
student
WHERE
s_name LIKE '%风%'
# 2.使用正则表达式
SELECT
*
FROM
student
WHERE
s_name REGEXP '风'
在 MySQL 中,LIKE 操作符用于在文本字段中搜索特定的模式。如果需要在文本字段中匹配通配符本身,可以使用反斜杠字符转义通配符。例如,如果要在一个名为 ’mytable’ 的表中查找包含下划线字符的字符串,可以使用以下查询:
SELECT * FROM mytable WHERE mycolumn LIKE '%\_%' ESCAPE '\';
在上面的查询中,ESCAPE 关键字指定了转义字符为反斜杠,因此我们在通配符前添加了一个反斜杠字符。这将告诉 MySQL 仅匹配下划线字符本身,而不是作为通配符进行匹配。
SELECT
stu1.s_name,
tmp_t.cnt AS '同名人数'
FROM
student AS stu1
LEFT JOIN (
SELECT s_name, s_sex, count(*) AS cnt
FROM student
GROUP BY s_name, s_sex
) AS tmp_t
ON stu1.s_name = tmp_t.s_name AND stu1.s_sex = tmp_t.s_sex
WHERE
tmp_t.cnt > 1
# 1.使用模糊匹配
SELECT
*
FROM
student
WHERE
s_birth LIKE '1990%'
# 2.使用正则表达式
SELECT
*
FROM
student
WHERE
s_birth REGEXP '^1990'
SELECT
score.c_id,
course.c_name,
round(avg(s_score), 2) AS avg_score
FROM
score, course
where score.c_id = course.c_id
GROUP BY
c_id
ORDER BY
avg_score DESC,
c_id ASC
SELECT
student.s_id,
student.s_name,
round(avg(s_score), 2) AS '平均成绩'
FROM
student
INNER JOIN score ON student.s_id = score.s_id
GROUP BY
score.s_id,
student.s_id,
student.s_name
HAVING
avg(score.s_score) > 85
SELECT
s_name,
s_score
FROM
student,
score
WHERE
student.s_id = score.s_id
AND c_id IN (SELECT c_id FROM course WHERE c_name = '数学')
AND s_score < 60
SELECT
student.s_id,
student.s_name,
course.c_name,
score.s_score
FROM
student,
course,
score
WHERE
student.s_id = score.s_id
AND score.c_id = course.c_id
ORDER BY
s_id
SELECT
student.s_name,
course.c_name,
score.s_score
FROM
student,
score,
course
WHERE
student.s_id = score.s_id
AND score.c_id = course.c_id
AND s_score > 70
SELECT
student.s_name,
course.c_name,
score.s_score
FROM
student,
score,
course
WHERE
student.s_id = score.s_id
AND score.c_id = course.c_id
AND s_score < 60
SELECT
student.s_id,
s_name
FROM
student,
score
WHERE
student.s_id = score.s_id
AND c_id = '01'
AND s_score >= 80
SELECT
c_name,
count(s_id) AS '学生人数'
FROM
score,
course
WHERE
score.c_id = course.c_id
GROUP BY
score.c_id
# 这里默认的是一门老师只教授一门课程
SELECT
student.*,
score.s_score
FROM
student,
score
WHERE
student.s_id = score.s_id
AND c_id IN (SELECT c_id FROM course WHERE t_id IN (SELECT t_id FROM teacher WHERE t_name = '张三'))
ORDER BY
s_score DESC
LIMIT 1
SELECT
sc1.s_id,
sc1.c_id,
sc2.c_id,
sc1.s_score,
sc2.s_score
FROM
score AS sc1,
score AS sc2
WHERE
sc1.s_id = sc2.s_id
AND sc1.s_score = sc2.s_score
AND sc1.c_id != sc2.c_id
SELECT
sc1.c_id,
sc1.s_id,
count(sc2.s_score) + 1 AS rank
FROM
score AS sc1
LEFT JOIN score AS sc2 ON sc1.c_id = sc2.c_id
AND sc1.s_score < sc2.s_score
GROUP BY
sc1.c_id,
sc1.s_score,
sc1.s_id
HAVING
count(sc2.s_score) < 2
ORDER BY
sc1.c_id,
rank
SELECT
c_id,
count(*) AS '选修人数'
FROM
score
GROUP BY
c_id
HAVING
count(*) > 5
ORDER BY
'选修人数' DESC,
c_id ASC
SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) >= 2
SELECT
*
FROM
student
WHERE
# SELECT count(*) FROM course) 查询的是总课程的数量
s_id IN (SELECT s_id FROM score GROUP BY s_id HAVING count(c_id) = (SELECT count(*) FROM course))
# 1.按照年份来计算
SELECT
s_id,
s_name,
(YEAR(now()) - YEAR(s_birth)) AS age
FROM
student
/*
2.使用 timestampdiff()
(1) TIMESTAMPDIFF(): 第一个参数设置时间单位,可以精确到年(YEAR)、天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND)。对于比较
的两个时间,时间小的放在前面,时间大的放在后面。
(3) datediff(): 返回值是相差的天数,无法定位到小时、分钟和秒。
*/
SELECT
s_id,
s_name,
timestampdiff(YEAR, s_birth, now()) AS age
FROM
student
# week(时间): 默认从 0 开始,表示星期天为一个星期的第一天,国外算法
# week(时间, 1): 从 1 开始,表示星期一为一个星期的第一天,国内算法
SELECT
s_id,
s_name
FROM
student
WHERE
WEEK (s_birth) = WEEK (now(), 1)
SELECT
s_id,
s_name
FROM
student
WHERE
WEEK (s_birth) = WEEK (now(), 1) + 1
SELECT
s_id,
s_name
FROM
student
WHERE
MONTH (s_birth) = MONTH (now())
SELECT
s_id,
s_name
FROM
student
WHERE
(MONTH(s_birth) = (((MONTH(NOW()) + 12) % 12) + 1))