1

I have the following SQL statement

SELECT 
      x_category.id, 
      x_category.name, 
      x_category.parent
FROM x_listings, x_category
WHERE x_listings.status = 1 
AND 
   (x_listings.cat_1_id = x_category.id OR
    x_listings.cat_2_id = x_category.id OR 
    x_listings.cat_3_id = x_category.id) 
GROUP BY x_category.id
ORDER BY x_category.id, x_category.parent  

This returns:

id, name, parent
1, Toys, 0
2, Clothes, 0
3, Hardware, 0
4, Educational Toys, 1
5, Women's Clothes, 2
6, Men's Clothes, 2
7, Hammers, 3

However, I want the results to come back ordered as such:

id, name, parent
1, Toys, 0
4, Educational Toys, 1
2, Clothes, 0
5, Women's Clothes, 2
6, Men's Clothes, 2
3, Hardware, 0
7, Hammers, 3

How can I modify the SQL statement to achieve this?

Thanks

Rigel1121
  • 2,022
  • 1
  • 17
  • 24
Martin
  • 795
  • 1
  • 12
  • 31
  • 1
    See the first answer to the question I marked as duplicate; It should look something like this: http://www.sqlfiddle.com/#!2/ca414/1 you should be able to easily alter the example to your query. – jpw Feb 17 '15 at 01:25

0 Answers0