Wednesday, 21 August 2013

Order by with Group by MySQL with duplicates

Order by with Group by MySQL with duplicates

I have a table which stores product visits and inserts the Product id into
a database every time that product is visited. The table is as follows:
CREATE TABLE IF NOT EXISTS member_preferences_products_data ( MPD_ID
int(10) NOT NULL AUTO_INCREMENT, MPD_Date date NOT NULL, MPD_Time time NOT
NULL, Member_ID int(10) NOT NULL, MP_ID int(10) NOT NULL, Product_ID
int(10) NOT NULL, PRIMARY KEY (MPD_ID) ) ENGINE=InnoDB DEFAULT
CHARSET=latin1 AUTO_INCREMENT=4266 ;
I need to query the top prroducts and my code is as follows:
SELECT *, member_preferences_products_data.Product_ID as UniqueProduct
FROM member_preferences_products_data
LEFT join products ON
member_preferences_products_data.Product_ID =
products.Product_ID
WHERE member_preferences_products_data.MPD_Date BETWEEN
'2013-08-10' AND '2013-08-21'
GROUP BY UniqueProduct
ORDER BY COUNT(member_preferences_products_data.Product_ID)
LIMIT 300
The query works but it doesn't appear to the be the right results. It's
not ordering by the most occuring Product ID between the time frame. It
seems to be taking another order.
Any ideas>?

No comments:

Post a Comment