MYSQL生日查询

时间:2016-03-04 08:38:38

标签: mysql

所以即使我的查询有问题,它选择接下来的三个生日,但是如果有一个生日是在4月12日。一旦4月1日到来,生日就不再显示了。

SELECT `users`.`name`, 
       Date_format(`users_detail`.`dob_date`, '%d') AS day, 
       Date_format(`users_detail`.`dob_date`, '%M') AS month 
FROM   `users_detail` 
       JOIN `users` 
         ON `users`.`id` = `users_detail`.`id` 
WHERE  Date_add(`users_detail`.`dob_date`, 
              INTERVAL Year( Curdate() )- Year(`users_detail`.`dob_date`) + IF( 
              Dayofyear( 
              Curdate() ) >= Dayofyear(`users_detail`.`dob_date`), 1, 0 ) year) 
       BETWEEN Curdate() AND Date_add(Curdate(), INTERVAL 11 month) 
ORDER  BY `users_detail`.`dob_date` ASC 
LIMIT  3

1 个答案:

答案 0 :(得分:0)

你能试试吗?

SELECT `users`.`name`
FROM  `users_detail`
WHERE  DATE_ADD(`users_detail`.`dob_date`, 
                INTERVAL YEAR(CURDATE())-YEAR(`users_detail`.`dob_date`)
                         + IF(DAYOFYEAR(CURDATE()) > DAYOFYEAR(`users_detail`.`dob_date`),1,0)
                YEAR)  
            BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 11 month)
ORDER  BY `users_detail`.`dob_date` ASC 
LIMIT  3