简体   繁体   中英

Join and count rows value in mysql?

I have two table in my database(first name - game_table and second name - attendee_table):

/ ---game_table-- \                   / -- Attendee_table -- \
| g_id | g_name   |               | a_id | g_id | a_name  | a_age |
| 101  | football |               |   1  | 101  | Santosh |   31  |
| 102  | Cricket  |               |   2  | 101  | Parveen |   35  |
| 102  | Hockey   |               |   3  | 101  | Ram     |   15  |
                                  |   4  | 101  | Radhe   |   10  |
                                  |   5  | 101  | Salim   |   31  |
                                  |   6  | 101  | sandeep |   25  |
                                  |   7  | 101  | Rahul   |   40  |

I want following result:

| age_break_up  | Count |
| 0 - 18 years  |   2   |
| 19 - 25 years |   1   |
| 26 - 40 years |   4   |
| 41+ years     |   0   |

so for that i am using following query :

mysqli_query($con, "SELECT f.a_age, COUNT(f.a_age) FROM game_table t INNER JOIN attendee_table f ON t.g_id = f.g_id GROUP BY f.a_age")

But this query return the following output:

| a_age | Count |
|  10   |   1   |
|  15   |   1   |
|  25   |   1   |
|  31   |   2   |
|  35   |   1   |
|  40   |   1   |

so how can i take my result like below:

| age_break_up  | Count |
| 0 - 18 years  |   2   |
| 19 - 25 years |   1   |
| 26 - 40 years |   4   |
| 41+ years     |   0   |

If there is no pattern on splitting the groups (EG groups of 18 years which is not the case) then I think you have to use conditional grouping, which can be done using CASE EXPRESSION :

SELECT CASE WHEN f.a_age < 19 THEN '0 - 18 years'
            WHEN f.a_age between 19 and 25 THEN '19 - 25 years' 
            WHEN f.a_age between 26 and 40 THEN '26 - 40 years'
            ELSE '41+ years'
       end as age_break_up, 
       COUNT(f.a_age) 
FROM game_table t 
INNER JOIN attendee_table f
 ON t.g_id = f.g_id 
GROUP BY CASE WHEN f.a_age < 19 THEN '0 - 18 years'
              WHEN f.a_age between 19 and 25 THEN '19 - 25 years' 
              WHEN f.a_age between 26 and 40 THEN '26 - 40 years'
              ELSE '41+ years'
         END

you can use a select case

mysqli_query($con, "SELECT 
                   case when  f.a_age between 0 and 18 then '0 - 18 years'
                        when  f.a_age between 19 and 25 then '19 - 25 years'
                        when  f.a_age between 26 and 40 then '26 - 40 years'
                                          else '26 - 40 years' 
                    end as age_break_up
                   , COUNT(f.a_age) 
                FROM game_table t 
                INNER JOIN attendee_table f ON t.g_id = f.g_id 
                GROUP BY  case when  f.a_age between 0 and 18 then '0 - 18 years'
                        when  f.a_age between 19 and 25 then '19 - 25 years'
                        when  f.a_age between 26 and 40 then '26 - 40 years'
                                          else '26 - 40 years' 
                    end ;")

Table game_table does not seem to play an important role for the purposes of your query. You have to use an in-line age 'bucket' table as the first table of your query, then LEFT JOIN attendee_table to it, so that you get a row returned even for age buckets having no related records:

SELECT CASE t1.age_bucket
          WHEN 1 THEN '0 - 18 years'
          WHEN 2 THEN '19 - 25 years'
          WHEN 3 THEN '26 - 40 years'
          ELSE '41+ years'
       END AS age_break_up,    
       COUNT(t2.a_age)               
FROM (SELECT 1 AS age_bucket UNION ALL SELECT 2 UNION ALL
      SELECT 3 UNION ALL SELECT 4) AS t1
LEFT JOIN atendee_table AS t2 ON t1.age_bucket = IF(t2.a_age BETWEEN 0 AND 18, 1,
                                                   IF(t2.a_age BETWEEN 19 AND 25, 2,
                                                     IF(t2.a_age BETWEEN 26 AND 40, 3, 4)))
GROUP BY IF(a_age BETWEEN 0 AND 18, 1,
            IF(a_age BETWEEN 19 AND 25, 2,
               IF(a_age BETWEEN 26 AND 40, 3, 4))) 

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