blakekr Posted June 22, 2009 Share Posted June 22, 2009 I apologize in advance for the largeness of this query. I'm using mysql 5.0.67. My problem is that the query below returns duplicate results for the same item if it appears in more than one category. For example I might have product AB-23423 that shows up in categories 16 and 103. With the query below, I unfortunately pull out two copies of that product even though I only want one. SELECT sh_products.id AS id , sh_prod_categories.cat_id AS catid , sh_products.prod_id AS prod_id , sh_products.prod_name AS prod_name , sh_products.prod_vendor AS prod_vendor , sh_products.prod_thumb AS prod_thumb , sh_products.prod_price AS prod_price , sh_categories.cat_title AS category FROM sh_products INNER JOIN sh_prod_categories on sh_products.prod_id = sh_prod_categories.prod_id INNER JOIN sh_categories on sh_prod_categories.cat_id = sh_categories.cat_id WHERE sh_categories.cat_id = 17 OR sh_categories.cat_id = 216 OR sh_categories.cat_id = 16 OR sh_categories.cat_id = 18 OR sh_categories.cat_id = 103 ORDER BY REPLACE(REPLACE(prod_name,'"',''),'\'','') DESC Given that this statement is on the very edge of my abilities already, can someone suggest a tweak that would result in only one result, even when a product appears in two categories? Quote Link to comment https://forums.phpfreaks.com/topic/163285-solved-getting-a-few-duplicates-in-an-inner-join-query/ Share on other sites More sharing options...
J.Daniels Posted June 22, 2009 Share Posted June 22, 2009 Add DISTINCT SELECT DISTINCT sh_products.id ... Quote Link to comment https://forums.phpfreaks.com/topic/163285-solved-getting-a-few-duplicates-in-an-inner-join-query/#findComment-861505 Share on other sites More sharing options...
blakekr Posted June 23, 2009 Author Share Posted June 23, 2009 Simple and to the point! Thank you so, so, so much. Quote Link to comment https://forums.phpfreaks.com/topic/163285-solved-getting-a-few-duplicates-in-an-inner-join-query/#findComment-861710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.