简体   繁体   中英

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 . My table has around 100 rows of data but this is likely to increase.

I'd like to use PHP to output persons based on age groups, so:

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:

$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 .

I would handle this by assigning age groups in the MySQL query itself, and then just letting PHP display the result set. 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 .

Then use the following PHP code:

$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>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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