我想使用birth_date
显示年龄在60岁以上的所有客户。 bith_date
的类型为date
。这是birth_date
-> 1999-03-12
的格式。有人可以帮我解决我的问题吗?
$sql = "SELECT * FROM `tbl_beneficiary` INNER JOIN tbl_barangay ON
tbl_beneficiary.barangay_id = tbl_barangay.barangay_id WHERE
tbl_beneficiary.is_active = 'No' AND (DATEDIFF(NOW(),
tbl_beneficiary.birth_date) <= 60) ORDER BY
tbl_beneficiary.beneficiary_id";
答案 0 :(得分:0)
我尚未对其进行测试,但是我认为这应该可以工作(它使用表别名以方便阅读)
SELECT * FROM `tbl_beneficiary` be
INNER JOIN tbl_barangay ba ON be.barangay_id = ba.barangay_id
WHERE
be.is_active = 'No' AND YEAR( FROM_DAYS( TO_DAYS( NOW() ) - TO_DAYS( be.birth_date ) ) ) > 60
ORDER BY
be.beneficiary_id
或更简单
SELECT * FROM `tbl_beneficiary` be
INNER JOIN tbl_barangay ba ON be.barangay_id = ba.barangay_id
WHERE
be.is_active = 'No' AND TIMESTAMPDIFF(YEAR, be.birth_date, CURDATE()) > 60
ORDER BY
be.beneficiary_id
答案 1 :(得分:0)
您可以使用:
$sql = "SELECT * FROM `tbl_beneficiary` INNER JOIN tbl_barangay ON
tbl_beneficiary.barangay_id = tbl_barangay.barangay_id WHERE
tbl_beneficiary.is_active = 'No' AND tbl_beneficiary.birth_date<=DATE_SUB(NOW(),INTERVAL 60 Years) ORDER BY
tbl_beneficiary.beneficiary_id";