Jump to content

Archived

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

ldsmike88

MySQL Query: One of each

Recommended Posts

I have a MySQL database with a whole lot of products in it. Each product has it's own row. Each product has a make, model, year, description etc. There are over 20 products with the same make but I need to query the database and display each make only once. Anyone know how to do this? Thanks!

Mike

Share this post


Link to post
Share on other sites
Look into the use of "group by" clause or "distinct".

[a href=\"http://dev.mysql.com/doc/refman/4.1/en/group-by-modifiers.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/gro...-modifiers.html[/a]

[a href=\"http://dev.mysql.com/doc/refman/4.1/en/distinct-optimization.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/dis...timization.html[/a]

You also want to add the "order by" clause to have it sorted by what you want (make).

Share this post


Link to post
Share on other sites
AWESOME! The GROUP BY worked perfect. Now I have one more question. Some of my products are compatible with multiple makes, such as the Chevy Silverado and the GMC Sierra. Most of their parts can fit the either truck. So in this case I set the make to "Chevorlet, GMC" istead of having two different rows for the same product and my query says WHERE make LIKE '%Chevorlet%'. With the Chevorlet and GMC products when I run the GROUP BY query it looks like this:

Audi
Buick
Cadillac
Chevorlet
Chevorlet, GMC
Chrysler
Chysler
Ford
GMC
Honda

Is there a way I can get everything inbetween the commas or everything before and after a comma and then erase the comma? Thanks! And thanks again toplay for the GROUP BY documentation.

Share this post


Link to post
Share on other sites

×

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.