简体   繁体   English

根据出生日期将MySQL结果分组为年龄组

[英]Group MySQL results into age groups based on date of birth

I have a MySQL table, persons , with 3 columns, id , name & date_of_birth . 我有一个MySQL表persons ,具有3列idnamedate_of_birth My table has around 100 rows of data but this is likely to increase. 我的表有大约100行数据,但是这可能会增加。

I'd like to use PHP to output persons based on age groups, so: 我想使用PHP根据年龄段输出人员,因此:

20-25: 10
26-35: 15
36-45: 12
46-55: 20
55+: 30

Right now, I am just using PHP to grab all the dates of birth using this piece of code: 现在,我只是使用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>';
}

Could someone guide me in the right direction to handle grouping my results by age group? 有人可以指导我朝正确的方向处理按年龄段分组的结果吗?

I did find this in another SO thread but not sure if it would be useful in the above context: SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age . 我确实在另一个SO线程中找到了它,但不确定在上述情况下是否有用: SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age

I would handle this by assigning age groups in the MySQL query itself, and then just letting PHP display the result set. 我可以通过在MySQL查询本身中分配年龄组来解决此问题,然后让PHP显示结果集。 You can try the following query: 您可以尝试以下查询:

SELECT t.age_group, COUNT(*) AS age_count
FROM
(
    SELECT
        CASE WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN 20 AND 25
             THEN '20-25'
             WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN 26 AND 35
             THEN '26-35'
             WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN 36 AND 45
             THEN '36-45'
             WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN 46 AND 55
             THEN '46-55'
             WHEN TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) > 55
             THEN '46-55'
             ELSE 'Other'
        END AS age_group
    FROM persons
) t
GROUP BY t.age_group

The inner query assigns an age group to each record, and then the outer query aggregates by age group. 内部查询为每个记录分配一个年龄组,然后外部查询按年龄组进行汇总。 Note that the nested subquery is not necessary, nor will it do much good for performance, but is used to avoid having to repeat the ugly CASE expression when using GROUP BY . 请注意,嵌套子查询不是必需的,它也不会对性能产生很大的好处,但是它用于避免在使用GROUP BY时必须重复难看的CASE表达式。

Then use the following PHP code: 然后使用以下PHP代码:

$sql2 = "...";  // use the above query
$result2 = $conn->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
    $age_group = $row2['age_group']);
    $age_count = $row2['age_count']);
    echo $age_group.': '.$age_count;
}
    SELECT CONCAT((FLOOR(`year`/5))*5,'-',((FLOOR(`year`/5))*5)+4) `range`,
    COUNT(*) qty 
    FROM persons  
    GROUP BY `range`

Try this: 尝试这个:

$count_20_25 = 0;
$count_26_35 = 0;
$count_36_45 = 0;
$count_46_55 = 0;
$count_above_55 = 0;
$curr_year = date('Y');

$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];
    $temp = abs($curr_year - $year);
    if($temp >= 20 && $temp <= 25)
    {
        $count_20_25++;
    }
    elseif($temp >= 25 && $temp <= 35)
    {
        $count_26_35++;
    }
    elseif($temp >= 36 && $temp <= 45)
    {
        $count_36_45++;
    }
    elseif($temp >= 46 && $temp <= 55)
    {
        $count_46_55++;
    }
    else
    {
        $count_above_55++;
    }
}

echo '20-25: '.$count_20_25.'<br>';
echo '26-35: '.$count_26_35.'<br>';
echo '36-45: '.$count_36_45.'<br>';
echo '46-55: '.$count_46_55.'<br>';
echo '55+: '.$count_above_55.'<br>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM