我有一個MySQL表,persons
,有3列,id
,name
& date_of_birth
。 我的表格有大約100行數據,但這可能會增加。根據出生日期將MySQL結果分組到年齡組中
我想用PHP基於年齡組輸出者,所以:
20-25: 10
26-35: 15
36-45: 12
46-55: 20
55+: 30
現在,我只是用PHP中使用這段代碼搶出生的所有日期:
$sql2 = "SELECT date_of_birth FROM persons";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$date_of_birth = explode('-', $row2['date_of_birth']);
$year = $date_of_birth[0];
$month = $date_of_birth[1];
$day = $date_of_birth[2];
echo 'Year of Birth: '.$year.'<br>';
}
有人能指導我在正確的方向來處理按年齡組分組我的結果嗎?
我確實在另一個SO線程中找到了這個,但不確定它在上述上下文中是否有用:SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
。
哇,蒂姆 - 不可思議!非常感謝你:-)我現在要做出必要的調整。非常感謝你今天的時間。 – michaelmcgurk