赞
踩
)
--建表 --学生表 CREATE TABLE `Student`( `s_id` VARCHAR(20), `s_name` VARCHAR(20) NOT NULL DEFAULT '', `s_birth` VARCHAR(20) NOT NULL DEFAULT '', `s_sex` VARCHAR(10) NOT NULL DEFAULT '', PRIMARY KEY(`s_id`) ); --课程表 CREATE TABLE `Course`( `c_id` VARCHAR(20), `c_name` VARCHAR(20) NOT NULL DEFAULT '', `t_id` VARCHAR(20) NOT NULL, PRIMARY KEY(`c_id`) ); --教师表 CREATE TABLE `Teacher`( `t_id` VARCHAR(20), `t_name` VARCHAR(20) NOT NULL DEFAULT '', PRIMARY KEY(`t_id`) ); --成绩表 CREATE TABLE `Score`( `s_id` VARCHAR(20), `c_id` VARCHAR(20), `s_score` INT(3), PRIMARY KEY(`s_id`,`c_id`) ); --插入学生表测试数据 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 Course values('01' , '语文' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03'); --教师表测试数据 insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('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);
主表中有的主键对应的列值,从表中可以没有和这个列值对应的数据,
主表中没有的主键对应的列值,从表中一定不能有这个列值对应的数据
join连接关系
先分组,再排序
查询书写格式
select...(avg(),sum())from...left join...on...
where...group by...
order by...asc/desc
limit ...
执行顺序
form...on...left join...where...
group by...
avg(),sum()...having...
select...
order by...asc/desc
limit...
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT a.*, b.s_score AS 01_score, c.s_score AS 02_score FROM
student a JOIN score b ON a.s_id = b.s_id AND b.c_id = '01'
LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id = '02' OR c.c_id = NULL WHERE b.s_score > c.s_score;
-- 或者
SELECT * (SELECT a.*, b.s_score AS 01_score, c.s_score AS 02_score FROM
student a JOIN score b ON a.s_id = b.s_id AND b.c_id = '01'
LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id = '02' OR c.c_id = NULL) d WHERE d.01_01_score > d.02_score;
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select a.* ,b.s_score as 01_score,c.s_score as 02_score from
student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL
join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from
student b
join score a on b.s_id = a.s_id
GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60;
sql查询语句结构
sql语句执行顺序:
无作为连接 先from–>where–>group by–>having–>avg , sum --> select–>order by
有连接查询 form–>on–>join–>where–>group by–>avg,sum–>having–>select–>distinct–>order by–>limit
having 后面跟的条件, 不要写select 中起的别名, 要写起别名之前的原名
原表的别名, 后面都可以使用, 因为是先from 表名 别名
根据平均成绩排序: order by 的条件,
模糊查询
排序
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
-- (包括有成绩的和无成绩的)
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from
student b
left join score a on b.s_id = a.s_id
GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60
union all
select a.s_id,a.s_name,0 as avg_score from
student a
where a.s_id not in (
select distinct s_id from score);
以上原理是一样的, 就是可以在原有查询数据基础上,添加列明&列值
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score --4
from student a ---1
left join score b on a.s_id=b.s_id--2
GROUP BY a.s_id,a.s_name;---3
-- 6、查询"李"姓老师的数量
select count(t.t_id) from teacher t where t.t_name like '%李%' group by t.t_id;
-- 7、查询学过"张三"老师授课的同学的信息
SELECT stu.* FROM student stu LEFT JOIN score sco ON stu.s_id = sco.s_id WHERE sco.c_id = (SELECT c.c_id FROM course c WHERE c.t_id=(SELECT t.t_id FROM teacher t WHERE t.t_name='张三'));
-- 8、查询没学过"张三"老师授课的同学的信息
SELECT s.* FROM student s WHERE s.s_id NOT IN
(
SELECT stu.s_id FROM student stu LEFT JOIN score sco ON stu.s_id = sco.s_id
WHERE sco.c_id =(SELECT c.c_id FROM course c LEFT JOIN teacher t ON c.t_id=t.t_id WHERE t.t_name='张三')
)
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT a.* FROM student a
LEFT JOIN score b ON a.s_id = b.s_id
LEFT JOIN score c ON a.s_id = c.s_id
WHERE b.c_id = '01' AND c.c_id ='02';
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT * FROM student WHERE s_id IN
(SELECT s_id FROM score WHERE c_id = '01') AND
s_id NOT IN (SELECT s_id FROM score WHERE c_id = '02')
where xxx is null 或者where xxx is not null
-- 11、查询没有学全所有课程的同学的信息
SELECT a.* FROM student a LEFT JOIN score b ON a.s_id = b.s_id GROUP BY a.s_id
HAVING a.s_id NOT IN
(SELECT s_id AS cts FROM score GROUP BY s_id HAVING COUNT(*) = 3);
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT * FROM student WHERE s_id IN
(SELECT s_id FROM score WHERE c_id IN
(SELECT c_id FROM score WHERE s_id = '01') GROUP BY s_id);
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT * FROM student WHERE s_id IN
(SELECT a.s_id FROM
(SELECT s_id, COUNT(*) AS cts FROM score WHERE c_id IN
(SELECT c_id FROM score WHERE s_id = '01') GROUP BY s_id
HAVING cts = (SELECT COUNT(*) FROM score WHERE s_id = '01')) a WHERE a.s_id !='01')
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT s_name FROM student WHERE s_id NOT IN
(SELECT s_id FROM score WHERE c_id =
(SELECT c_id FROM course WHERE t_id =
(SELECT t_id FROM teacher WHERE t_name = '张三')));
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT a.s_id, a.s_name, AVG(b.s_score) FROM student a
LEFT JOIN score b ON a.s_id = b.s_id WHERE b.s_score < 60 OR b.s_score IS NULL
GROUP BY a.s_id;
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT a.*, b.c_id, b.s_score FROM student a
LEFT JOIN score b ON a.s_id = b.s_id
WHERE b.c_id = '01' AND b.s_score < 60
ORDER BY b.s_score DESC;
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT tempA.*, tempB.avgs FROM
(SELECT a.*, b.s_score AS '01', c.s_score AS '02', d.s_score AS '03' FROM student a
LEFT JOIN score b ON a.s_id = b.s_id AND b.c_id = '01'
LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id = '02'
LEFT JOIN score d ON a.s_id = d.s_id AND d.c_id = '03') tempA LEFT JOIN
(SELECT a.s_id, ROUND(AVG(b.s_score),2) AS avgs FROM student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY a.s_id
ORDER BY avgs DESC) tempB ON tempA.s_id = tempB.s_id;
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
//及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT s.c_id AS 课程ID, c.c_name AS 课程NAME, ROUND(MAX(s.s_score),2) AS 最高分, ROUND(MIN(s.s_score),2) AS 最低分, ROUND(AVG(s.s_score),2) AS 平均分,
ROUND((SELECT SUM(CASE WHEN b.s_score >= 60 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 及格率,
ROUND((SELECT SUM(CASE WHEN b.s_score >= 70 AND b.s_score < 80 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 中等率,
ROUND((SELECT SUM(CASE WHEN b.s_score >= 80 AND b.s_score < 90 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 优良率,
ROUND((SELECT SUM(CASE WHEN b.s_score >= 90 THEN 1 ELSE 0 END) FROM score b WHERE b.c_id = s.c_id)/
(SELECT COUNT(*) FROM score a WHERE a.c_id = s.c_id),2) AS 优秀率
FROM score s LEFT JOIN course c ON s.c_id = c.c_id
GROUP BY s.c_id;
基本语法
select xxx, xxx, -- 一定要有逗号
case
when 条件1 then 条件1查询结果对应的列值,
when 条件2 then 条件2查询结果对应的列值,
else 不符合以上条件对应的列值,
end 查询结果对应的列名 -- 一定不可以有逗号
from 表名;
case when … else… end… 就是把查询结果单独赋值,单独赋列名
-- 19、按各科成绩进行排序,并显示排名
-- 成绩相同不并列
SELECT ss01.*, @rank1 := @rank1 + 1 AS rank
FROM (SELECT c_id, s_score, s_id FROM score WHERE c_id = '01' ORDER BY s_score DESC) ss01,
(SELECT @rank1 := 0) r
UNION ALL
SELECT ss02.*, @rank2 := @rank2 + 1 AS rank
FROM (SELECT c_id, s_score, s_id FROM score WHERE c_id = '02' ORDER BY s_score DESC) ss02,
(SELECT @rank2 := 0) r
UNION ALL
SELECT ss03.*, @rank3 := @rank3 + 1 AS rank
FROM (SELECT c_id, s_score, s_id FROM score WHERE c_id = '03' ORDER BY s_score DESC) ss03,
(SELECT @rank3 := 0) r;
语法
-- 1,2,3 顺序排名
select @rank := @rank + 1 as rank from (select * from 表名 where xxx=xxx order by xxx desc) 表名, (select @rank := 0) r
-- 1,1,2 并列排名
select t1.*,
(case when @avgScore = t1.s_score then @rank
when @avgScore := t1.s_score THEN @rank := @rank+1 end) as rank
from (SELECT * FROM score WHERE c_id= '01' ORDER BY s_score DESC) t1,
(SELECT @rank := 0, @avgScore := 0)
union all使用
order by & union all 使用
rank使用
-- 20、查询学生的总成绩并进行排名 ?? 并列排名
SELECT s.s_id, s.sums, @rank := @rank + 1 AS 总成绩排名
FROM (SELECT s_id, SUM(s_score) AS sums FROM score GROUP BY s_id ORDER BY sums DESC) s,
(SELECT @rank := 0) r
-- 21、查询不同老师所教不同课程平均分从高到低显示
SELECT c.t_id, c.c_id, c.c_name,t.t_name, AVG(s.s_score) AS avgs FROM score s
LEFT JOIN course c ON s.c_id = c.c_id
LEFT JOIN teacher t ON c.t_id = t.t_id
GROUP BY t.t_id
ORDER BY avgs DESC
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩(采取的是1,1,2的方式进行排名) SELECT student.*,tt.c_id,tt.s_score,tt.rank FROM ( SELECT t1.*, (CASE WHEN @avgScore = t1.s_score THEN @rank1 WHEN @avgScore := t1.s_score THEN @rank1 := @rank1+1 END) AS rank FROM (SELECT * FROM score WHERE c_id= '01' ORDER BY s_score DESC) t1,(SELECT @rank1:=0,@avgScore:=0)r UNION ALL SELECT t1.*, (CASE WHEN @avgScore = t1.s_score THEN @rank2 WHEN @avgScore := t1.s_score THEN @rank2 := @rank2+1 END) AS rank FROM (SELECT * FROM score WHERE c_id= '02' ORDER BY s_score DESC) t1,(SELECT @rank2:=0,@avgScore:=0)r UNION ALL SELECT t1.*, (CASE WHEN @avgScore = t1.s_score THEN @rank3 WHEN @avgScore := t1.s_score THEN @rank3 := @rank3+1 END) AS rank FROM (SELECT * FROM score WHERE c_id= '03' ORDER BY s_score DESC) t1,(SELECT @rank3:=0,@avgScore:=0)r ) tt LEFT JOIN student ON tt.s_id = student.s_id WHERE tt.rank = 2 OR tt.rank = 3
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 SELECT c.*, (SELECT COUNT(*) FROM score s WHERE s.s_score >= 85 AND s.s_score <= 100 AND s.c_id = c.c_id) AS `[100-85]`, CONCAT( ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 85 AND s.s_score <= 100 AND s.c_id = c.c_id)/ (SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2), '%') AS '[100-85]占比', (SELECT COUNT(*) FROM score s WHERE s.s_score >= 70 AND s.s_score < 85 AND s.c_id = c.c_id) AS `[85-70]`, CONCAT( ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 70 AND s.s_score < 85 AND s.c_id = c.c_id)/ (SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2), '%') AS '[85-70]占比', (SELECT COUNT(*) FROM score s WHERE s.s_score >= 60 AND s.s_score < 70 AND s.c_id = c.c_id) AS '[70-60]', CONCAT( ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 60 AND s.s_score < 70 AND s.c_id = c.c_id)/ (SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2), '%') AS '[70-60]占比', (SELECT COUNT(*) FROM score s WHERE s.s_score >= 0 AND s.s_score < 60 AND s.c_id = c.c_id) AS '[60-0]', CONCAT( ROUND(100 * (SELECT COUNT(*) FROM score s WHERE s.s_score >= 0 AND s.s_score < 60 AND s.c_id = c.c_id)/ (SELECT COUNT(*) FROM score s WHERE s.c_id = c.c_id),2), '%') AS '[60-0]占比' FROM course c;
字符串拼接函数
SELECT CONCAT (s_score,‘分’) as 成绩 FROM score;
字符串拼接, 查出来的结果拼接一个字符串
-- 24、查询学生平均成绩及其名次 采取的是(1,1,2)的方式排序
SELECT temp.*,
(CASE WHEN @avgs = temp.avgs THEN @rank
WHEN @avgs := temp.avgs THEN @rank := @rank + 1 END) AS rank FROM
(SELECT s.s_id, ROUND(AVG(s.s_score),2) AS avgs FROM score s
LEFT JOIN student stu ON s.s_id = stu.s_id
GROUP BY s.s_id ORDER BY avgs DESC) temp,
(SELECT @avgs := NULL, @rank := 0) r;
-- 25、查询各科成绩前三名的记录
select * from score a where 3>(select count(*) from score where a.c_id = c_id and s_score > a.s_score ) order by a.c_id,s_score desc
-- 26、查询每门课程被选修的学生数
SELECT s.c_id, COUNT(*) AS 选修人数 FROM score s GROUP BY s.c_id;
-- 27、查询出只有两门课程的全部学生的学号和姓名
SELECT s.s_id, s.s_name FROM student s LEFT JOIN score sco ON s.s_id = sco.s_id
GROUP BY s.s_id HAVING COUNT(*) = 2;
-- 28、查询男生、女生人数
SELECT s.s_sex, COUNT(*) AS nums FROM student s GROUP BY s.s_sex
-- 29、查询名字中含有"风"字的学生信息
SELECT s.* FROM student s WHERE s.s_name LIKE '%风%'
-- 30、查询同名同性学生名单,并统计同名人数
SELECT s.s_name, COUNT(*) AS nums FROM student s GROUP BY s.s_name HAVING nums > 1
-- 31、查询1990年出生的学生名单
SELECT * FROM student WHERE SUBSTR(s_birth FROM 1 FOR 4) = '1990'
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select s.c_id, round(avg(s.s_score),2) as avgs from score s group by s.c_id order by avgs desc, s.c_id asc;
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT stu.s_id, stu.s_name, ROUND(AVG(s.s_score),2) AS avgs FROM score s LEFT JOIN student stu
ON s.s_id = stu.s_id
GROUP BY s.s_id
HAVING avgs >= 85
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT stu.s_name, s.s_score FROM score s LEFT JOIN student stu
ON s.s_id = stu.s_id
WHERE s.c_id = (SELECT c_id FROM course WHERE c_name = '数学')
AND s.s_score < 60
-- 35、查询所有学生的课程及分数情况;
SELECT stu.*, c.c_name, s.s_score FROM score s
LEFT JOIN course c ON s.c_id = c.c_id
LEFT JOIN student stu ON s.s_id = stu.s_id
-- 36、查询任何一门课程成绩在70分以上的学生的姓名、课程名称和分数;
SELECT stu.*, c.c_name, s.s_score FROM score s
LEFT JOIN course c ON s.c_id = c.c_id
LEFT JOIN student stu ON s.s_id = stu.s_id
WHERE s.s_score > 70
-- 37、查询不及格的课程
SELECT s.s_id, s.c_id, c.c_name,s.s_score FROM score s
LEFT JOIN course c ON s.c_id = c.c_id
WHERE s.s_score < 60
-- 38、查询课程编号为01且课程成绩大于等于80分的学生的学号和姓名;
SELECT stu.s_id, stu.s_name, s.c_id, s.s_score FROM score s
LEFT JOIN student stu ON s.s_id = stu.s_id
WHERE s.c_id = '01' AND s.s_score >= 80
-- 39、求每门课程的学生人数
SELECT c_id, COUNT(s_id) AS cts FROM score GROUP BY c_id
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩(可能包含相同成绩)
SELECT stu.*, s.c_id, MAX(s.s_score) AS max_score FROM score s
LEFT JOIN student stu ON s.s_id = stu.s_id
WHERE c_id = (SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))_id
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT a.*
FROM score a
LEFT JOIN score b
ON a.s_id = b.s_id
WHERE a.c_id <> b.c_id
AND a.s_score = b.s_score
GROUP BY a.c_id;
-- 42、查询每门功成绩最好的前两名 SELECT temp.* FROM (SELECT s1.*, @rank1 := @rank1 + 1 AS rank FROM ( SELECT a.* FROM score a WHERE a.c_id = '01' ORDER BY a.s_score DESC) s1, (SELECT @rank1 := 0) r UNION ALL SELECT s2.*, @rank2 := @rank2 + 1 AS rank FROM ( SELECT a.* FROM score a WHERE a.c_id = '02' ORDER BY a.s_score DESC) s2, (SELECT @rank2 := 0) r UNION ALL SELECT s3.*, @rank3 := @rank3 + 1 AS rank FROM ( SELECT a.* FROM score a WHERE a.c_id = '03' ORDER BY a.s_score DESC) s3, (SELECT @rank3 := 0) r) temp WHERE temp.rank = 1 OR temp.rank = 2; -- 简单方法, 不明白 SELECT * FROM score s1 WHERE 2 > (SELECT COUNT(*) FROM score WHERE c_id = s1.c_id AND s_score > s1.s_score) ORDER BY c_id, s_score DESC
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。
-- 要求输出课程号和选修人数,查询结果按人数降序排列,
-- 若人数相同,按课程号升序排列
SELECT
s.c_id,
COUNT(*) AS cts
FROM score s
GROUP BY s.c_id
HAVING cts > 5
ORDER BY cts DESC, s.c_id ASC;
-- 44、检索至少选修两门课程的学生学号
SELECT
s.s_id,
COUNT(*) AS cts
FROM score s
GROUP BY s.s_id
HAVING cts >= 2;
-- 45、查询选修了全部课程的学生信息
SELECT
*
FROM student
WHERE s_id IN
(SELECT
s.s_id
FROM score s
GROUP BY s.s_id
HAVING COUNT(*) = 3)
-- 46、查询各学生的年龄
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT
*,
(CASE WHEN MONTH(s_birth) > MONTH(NOW()) THEN YEAR(NOW()) - YEAR(s_birth) -1
WHEN MONTH(s_birth) < MONTH(NOW()) THEN YEAR(NOW()) - YEAR(s_birth)
WHEN MONTH(s_birth) = MONTH(NOW()) AND DAY(s_birth) > DAY(NOW()) THEN YEAR(NOW()) - YEAR(s_birth) - 1
WHEN MONTH(s_birth) = MONTH(NOW()) AND DAY(s_birth) < DAY(NOW()) THEN YEAR(NOW()) - YEAR(s_birth)
ELSE NULL END) AS age
FROM student;
-- 47、查询本周过生日的学生
SELECT
*
FROM student
WHERE WEEKOFYEAR(s_birth) = WEEKOFYEAR(NOW())
-- 48、查询下周过生日的学生
SELECT
*
FROM student
WHERE (WEEKOFYEAR(s_birth) - WEEKOFYEAR(NOW()) = 1);
-- 49、查询本月过生日的学生
SELECT
*
FROM student
WHERE MONTH(s_birth) = MONTH(NOW());
-- 50、查询下月过生日的学生
SELECT
*
FROM student
WHERE (MONTH(s_birth) - MONTH(NOW()) = 1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。