Jump to content

Recommended Posts

 

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..  :confused:

 

Link to comment
https://forums.phpfreaks.com/topic/254940-mysql-distinct-or-group-by/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.