Jump to content

Order by and group


petenaylor

Recommended Posts

Hi all

 

I have a table which is attached that has prices and a product_id. What I need to do is firstly, order by the price and then group by the product_id.

 

I need to know how to write the mySQL query to do this. I can only group by and then sort by.

 

Many thanks for your help.

 

Pete

post-73756-13482403723849_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/267686-order-by-and-group/
Share on other sites

The mysql syntax rule is "GROUP BY ... ORDER BY ...".

 

GROUP BY is for aggregation and will result in a single row for each value of the column you group by, product_id in your case.

 

Perhaps if gave us an example of what you are trying to achieve.

Link to comment
https://forums.phpfreaks.com/topic/267686-order-by-and-group/#findComment-1373128
Share on other sites

Hi there

 

Thanks for your help so far!

 

Basically, I need to get my product IDs in order of price. So that I can query the database again to get the product information.

 

If you look at my database, you can see that the prices can be sorted by value ascending. I need to then group the product IDs in the correct order so I can then query the database again for the full information.

 

Many thanks

 

Pete

Link to comment
https://forums.phpfreaks.com/topic/267686-order-by-and-group/#findComment-1373132
Share on other sites

This sounds like you are going to be looping through the results of one query and performing further queries within loop. This is not a good design decision. Get the results you need first time, then process the results.

 

Which brings me back to my original question - what exactly is the final result that you want to achieve? From the data you gave us how do expect the final output to look?

Link to comment
https://forums.phpfreaks.com/topic/267686-order-by-and-group/#findComment-1373133
Share on other sites

Hi there

 

I have restructured the database to have the 5 prices in the same table as the rest of the data.

 

Is there anyway I can search multiple rows for the lowest value across the 5 columns and then show the results in this order?

 

I have attached my database.

 

Regards

Pete

post-73756-1348240372483_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/267686-order-by-and-group/#findComment-1373170
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.