0

Just wondering if this is possible at all. I have a table which has two columns, item and category which when I use the following SQL statement displays:

SELECT * from tableitems ORDER BY category;

Table:

item    category
aitem   AAA
aaitem  AAA
abitem  AAA

acitem  BBB
baitem  BBB
bcitem  BBB

aditem  CCC
bditem  CCC
caitem  CCC

Is there anyway in MySQL to display it like this in a single MySQL statement?

AAA
aitem   
aaitem  
abitem  
BBB
acitem  
baitem  
bcitem  
CCC
aditem  
bditem  
caitem 
4
  • 2
    @hafichuk How to GROUP BY help this situation? Nov 18, 2011 at 19:14
  • @Nonym This is not my question.... Nov 18, 2011 at 19:20
  • Arif :: so sorry :( @user1038814 :: you mean that you want data from both columns returned in one single column? Just making sure
    – Nonym
    Nov 18, 2011 at 19:24
  • @Nonym yes, that is my intention. I was just wondering if it was possible to do so with one sql statement, failing which I'll just use php to manipulate it. Sorry, just dabbling in DB, just a freshie. Thanks Nov 18, 2011 at 21:03

2 Answers 2

1

No, you can't do that with a simple SQL statement. You can easily arrange the information to look like that on any client language you are using to access MySQL though...

1

I'd advise against it, but it is possible:

SELECT IFNULL(item, category) AS value
FROM
(
    SELECT category, item FROM tableitems
    UNION ALL
    SELECT DISTINCT category, NULL FROM tableitems
) T1
ORDER BY category, item

Your Answer

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

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