MySQL是否可以根据年龄段处理数据分组?
在我的用户表中,年龄值是他们的实际年龄。我想根据年龄段对他们进行分组。例如:
ages below 1 year old as age1, 1-4 yrs as age2, 5-9 yrs. old as age3 and so on.
答案 0 :(得分:2)
是。您可以在CASE
分段中进行分区。
SELECT
COUNT(*),
CASE
WHEN age < 1 THEN 'age1'
WHEN age BETWEEN 1 AND 4 THEN 'age2'
WHEN age BETWEEN 4 AND 9 THEN 'age3'
ELSE 'age4'
END AS ageband
FROM children
GROUP BY ageband;