SELECT a.* FROM ( SELECT t.name,t.birthday,CASE WHEN t.b>0 THEN t.b ELSE t.a END days FROM ( SELECT NAME, birthday , DATEDIFF(CONCAT(DATE_FORMAT(NOW(),'%Y')+1,DATE_FORMAT(birthday,'-%m-%d')),NOW()) a, DATEDIFF(CONCAT(DATE_FORMAT(NOW(),'%Y'),DATE_FORMAT(birthday,'-%m-%d')),NOW()) b FROM tb_micro_member ) t ) a WHERE a.days BETWEEN 0 AND 40;