patrickin Posted August 1, 2010 Share Posted August 1, 2010 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 https://forums.phpfreaks.com/topic/209475-distinct-help/ Share on other sites More sharing options...
dreamwest Posted August 2, 2010 Share Posted August 2, 2010 you should still GROUP BY when selecting distinct eg: SELECT DISTINCT * FROM `table` WHERE `title`='funny' GROUP BY `title` Link to comment https://forums.phpfreaks.com/topic/209475-distinct-help/#findComment-1094052 Share on other sites More sharing options...
fenway Posted August 4, 2010 Share Posted August 4, 2010 you should still GROUP BY when selecting distinct eg: 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 https://forums.phpfreaks.com/topic/209475-distinct-help/#findComment-1095219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.