I have a messages table in a database, which include a sender id and a message type (and of course many more columns not relevant for this question). I try to create a query which counts how many messages of each type a user have send.
e.g. if I have the following table:
--------------------------- id | user_id | message_type --------------------------- 1 | 1 | private 2 | 1 | public 3 | 1 | private ---------------------------
Then I want to get the following:
--------------------- id | private | public --------------------- 1 | 2 | 1 ---------------------
So in fact I want to group by message_type and user_id, but instead of generating multiple rows per user, I want to create multiple columns, one for each message_type
Can I achieve this without hardcoding the message types in my query?