我想获得所有生日是下个月的学生(我已经看到了几个答案)只筛选那些将要成为21岁的学生,任何想法?我的表名是students
,列名是birthdate
(日期),谢谢!
答案 0 :(得分:2)
如果birthdate属于date类型,您可以使用以下内容:
SELECT *
FROM students
WHERE
birthdate BETWEEN
LAST_DAY(CURDATE())+INTERVAL 1 DAY - INTERVAL 21 YEAR AND
LAST_DAY(LAST_DAY(CURDATE())+INTERVAL 1 DAY) - INTERVAL 21 YEAR
答案 1 :(得分:2)
假设生日专栏属于日期类型,您可以使用以下条件筛选出20岁并且下个月有生日的学生。
SELECT * FROM students
WHERE MONTH(birthdate) = (MONTH(NOW()) % 12) + 1
AND
TIMESTAMPDIFF(YEAR, birthdate, NOW()) = 20
答案 2 :(得分:0)
试试这个:
SELECT * FROM students WHERE MONTH(birthdate)=(MONTH(CURDATE()) % 12 + 1) AND
(YEAR(CURDATE())-YEAR(birthdate) = 21) ;