GROUP BY more group in a single group cakephp

Discussion in 'PHP' started by glagokulan, Mar 20, 2013.

  1. #1
    I have 5 types of groups in my DB table. i need to group type 1,type 2 and type 3 as a single group, and type 4 as another group and type 5 as another one.

    How can i do this using cakephp1.3?

    Please help me anyone
     
    glagokulan, Mar 20, 2013 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    MySQL doesn't support grouping like you are needing to do unless you use CASE within the group by. I wouldn't do it this way because it would require you to re-edit the application code every time you need to change the grouping.

    You could make and join a second table with a mapping to the types and group by the column in that table. You would map type 1,2,3 to one value, and 4 and 5 to their own, and then group by that column. This way if you add types or need to modify the grouping, you just edit the mapped table.

    New table:
    type (corresponding with existing table)
    group_type

    You would add the data.
    type    group_type
    1        1
    2        1
    3        1
    4        2
    5        3
    PHP:
    Then group by the group type after doing an inner join to the new table.
     
    jestep, Mar 21, 2013 IP