l!m!t Posted January 13, 2012 Share Posted January 13, 2012 I have a list of items I am running through a PHP loop. The columns are (car_name, car category) . I want to retrieve all the car names and sort them under their defined category. (example: Category 1 Ford 1 Ford 2 Ford 3 Category 2 mazda 1 mazda 2 mazda 3 I tried - (select car_name from my_table group by category). However it seems to also group all the cars into one result. Basically I only want the category to show once and each car within that category to show below. Any idea what I am doing wrong? I tried using DISTINCT as well.. Quote Link to comment https://forums.phpfreaks.com/topic/254940-mysql-distinct-or-group-by/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2012 Share Posted January 13, 2012 You would not use either (assuming you are using one query.) Your goal for any SELECT query is a single query that gets the rows you want in the order that you want them. Then, when you are iterating over the rows in your presentation logic, you output the data the way you want. For what you want, you would remember the category value and output a new section heading when the category changes. See this post - http://www.phpfreaks.com/forums/index.php?topic=349740.msg1650897#msg1650897 Quote Link to comment https://forums.phpfreaks.com/topic/254940-mysql-distinct-or-group-by/#findComment-1307178 Share on other sites More sharing options...
fenway Posted January 16, 2012 Share Posted January 16, 2012 That's layout -- use a hash. Quote Link to comment https://forums.phpfreaks.com/topic/254940-mysql-distinct-or-group-by/#findComment-1308213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.