使用PHP在mysql中查询某个年龄BY出生日期

时间:2016-02-02 10:16:53

标签: php mysql

我创建了一个员工数据库,其中包含varchar格式(31-12-2016)或d-m-Y的出生日期。 我想找到58岁及以上的员工。我使用这个查询:

$age=mysql_db_query($db,"select * from employees where round(datediff(str_to_date(birth_day,'%d-%m-%Y'), curent_date())/365) > 58",@$koneksi);
$birth = mysql_num_rows($birth);

使用上面的查询不会显示任何内容。 查询有什么问题??

提前谢谢。 。

1 个答案:

答案 0 :(得分:1)

使用abs()将给出数字的绝对值。您的查询返回负值,因此您没有获得正确的记录。

尝试使用以下查询:

select * 
from employees 
where abs(round(datediff(str_to_date(birth_day,'%d-%m-%Y'), current_date())/365)) > 58"

您还使用curent_date代替current_date()