0

I have a rather complicated question (for me at least)

I have the following table structure

| id | roomtype | roomno | feature
| 1  |    STDK  |   11   |  BT
| 2  |    STDK  |   11   |  AE
| 3  |    STDK  |   22   |  SMK
| 4  |    STDK  |   22   |  BT
| 5  |    STDT  |   33   |  NONSMK

and I want to have an output like this

Type: STDK - RoomNo: 11 - Features: BT, AE
Type: STDK - RoomNo: 22 - Features: SMK, BT
Type: STDT - RoomNo: 33 - Features: NONSMK

It's probably not that complicated but I can't get it...

3 Answers 3

5

This shows the result you're looking for... But I don't like the idea of formatting the result in the query!

select
    concat("Type: ", roomtype,
    " - RoomNo: ", roomno,
    " - Features: ", group_concat(feature order by feature separator ', '))
    as result
from t1
group by roomtype, roomno

Here is a working example

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for not liking the idea of all that formatting in SQL and for teaching me about sqlize.com
Thanks ^.^ There are some other useful links in my profile (I'm lazy, I don't even bookmark them) :P
I have a couple jsfiddle.net boiler plate setups in mine.
Great thanks, that's what I was looking for, although I think I'll make some changes, since I don't like the idea of SQL formatting as well. GROUP_CONCAT was what I was looking for, the rest will be done by the php... cheers and thanks
3

You could use MySQL's group_concat:

select roomtype, roomno, group_concat(feature order by feature separator ', ')
from your_table
group by roomtype, roomno
order by roomno

Producing your fully formatted output from that is left as an exercise.

Comments

0
SELECT 
     roomtype AS Type, 
     roomno AS RoomNo, 
     GROUP_CONCAT(feature ORDER BY feature SEPARATOR ', ') AS Features
FROM 
    tableName
GROUP BY roomtype, roomno
ORDER BY roomno

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.