1

So I currently have an sql statement:

Select room_no, category from help_desk where room_no = '133' OR room_no = '120';

and it returns:

enter image description here

How would I write an sql statement that would return it like this

enter image description here

3

You need to use three things

  • GROUP BY
  • GROUP_CONCAT
  • DISTINCT within GROUP_CONCAT

Here is such a query

Select room_no, group_concat(distinct category) categories
from help_desk where room_no in (133,120) group by room_no;
2

You are looking for the group_concat feature.

Group Concat

For example:

SELECT room_no, GROUP_CONCAT(category)  
FROM help_desk 
GROUP BY room_no;  
  • How do I make sure they don't repeat (All unique categories). – apex Jul 21 '17 at 15:32
  • 1
    you can combine with distinct in the group_concat :) – Ollie Jul 21 '17 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.