Jump to content

MySQL Query: One of each


ldsmike88

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
Link to comment
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).
Link to comment
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.
Link to comment
Share on other sites

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.