Jump to content



Recommended Posts

I am querying my DB to get some listings.  But because  a listing can fall into up to 5 sub categories, they can be returned up to 5 times by the following query, however I need duplicates removed, via DISTINCT.  However I cannot figure out how to place DISTINCT in this query, so that it actually works, as I am still getting duplicates.  Currently no errors are being thrown out, I'm just getting dupes, and I think my SQL ver and table structure is irrelevant for this question.


SELECT DISTINCT listings.*, categories.ID as catID, subcategories.ID as subID, subcategories.categoryID as sccatID FROM listings, categories, subcategories WHERE listings.active = 1 AND subcategories.categoryID = 1 AND categories.ID = 1 AND (listings.subcategory1ID = subcategories.ID OR listings.subcategory2ID = subcategories.ID OR listings.subcategory3ID = subcategories.ID OR listings.subcategory4ID = subcategories.ID OR listings.subcategory5ID = subcategories.ID


Thanks for any help!

Link to comment
Share on other sites

you should still GROUP BY when selecting distinct




SELECT DISTINCT * FROM `table` WHERE `title`='funny' GROUP BY `title`


No, you shouldn't -- but DISTINCT * is meaningless.


In fact, don't ever use DISTINCT -- you'll thank me later.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

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.