开发者

Age range query in MySQL based on two age fields

开发者 https://www.devze.com 2023-02-04 03:16 出处:网络
I\'m losing some hair over this. I found several online threads with a similar issue, but not exactly what I am running into, so I am still a bit jammed.

I'm losing some hair over this. I found several online threads with a similar issue, but not exactly what I am running into, so I am still a bit jammed.

I am writing a very complex query (at least its complex for me :))

In my ages table, I have an age_start and age_end field.

For example:

age_start   |   age_end
------------------------
0      -    64
19     -     23
0      -     19
25     -     29
65     -     100
...

And what I am trying to achieve is a query whi开发者_开发问答ch reads in between these ranges.

The best I have come up with so far is having these two fields set as array values, and in my MySQL query do something like:

Enter Age: [ 33 ]

Get ages data into an array. And then query like:

...WHERE $age >= $age_start AND age <= $age_end...

Would return:

age_start   |   age_end
------------------------
0       -     64
25      -    29

Does anyone have any ideas or suggestion or a better approach to this?


SELECT *
  FROM ages
 WHERE 32 BETWEEN age_start AND age_end


SELECT
                        CASE
                            WHEN age < 13 THEN 'Under 13'
                            WHEN age BETWEEN 13 and 15 THEN '13 - 15'
                            WHEN age BETWEEN 16 and 20 THEN '16 - 20'
                            WHEN age BETWEEN 21 and 25 THEN '21 - 25'
                            WHEN age BETWEEN 26 and 30 THEN '26 - 30'
                            WHEN age BETWEEN 31 and 35 THEN '31 - 35'
                            WHEN age BETWEEN 36 and 40 THEN '36 - 40'
                            WHEN age BETWEEN 41 and 45 THEN '41 - 45'
                            WHEN age BETWEEN 46 and 50 THEN '46 - 50'
                            WHEN age BETWEEN 51 and 55 THEN '51 - 55'
                            WHEN age BETWEEN 51 and 55 THEN '56 - 60'
                            WHEN age BETWEEN 51 and 55 THEN '61 - 65'
                            WHEN age BETWEEN 51 and 55 THEN '66 - 70'
                            WHEN age BETWEEN 51 and 55 THEN '71 - 75'
                            WHEN age BETWEEN 51 and 55 THEN '76 - 80'
                            WHEN age BETWEEN 51 and 55 THEN '81 - 85'
                            WHEN age BETWEEN 51 and 55 THEN '86 - 90'
                            WHEN age BETWEEN 51 and 55 THEN '91 - 95'
                            WHEN age BETWEEN 51 and 55 THEN '96 - 100'
                            WHEN age >= 100 THEN 'Over 100'
                            WHEN age IS NULL THEN 'Not Filled In (NULL)'
                        END as age_range,
                        COUNT(ids) AS age_count,group_concat(ids) as chart_ids

                        FROM (SELECT 
                                TIMESTAMPDIFF(YEAR, `TABLE_FIELD_OF_DATE_OF_BIRTH`, CURDATE()) AS age, user_id as ids 
                           FROM 
                                jw_user 
                            WHERE 
                                is_active=1 
                            AND 
                                is_deleted = 0 
                        ) as derived

                    GROUP BY age_range

                    ORDER BY age_range

and the would be amazing :)

0

精彩评论

暂无评论...
验证码 换一张
取 消