
Originally Posted by
numinix
I'm working on a simple module that loads the best sellers from a given category from the previous x months. Here is the SQL query:
PHP Code:
SELECT DISTINCT (p2c.products_id), SUM( op.products_quantity ) AS products_ordered, pd.products_name, p.products_image
FROM products_to_categories p2c
LEFT JOIN orders_products op ON ( op.products_id = p2c.products_id )
LEFT JOIN orders o ON ( o.orders_id = op.orders_id )
LEFT JOIN products_description pd ON ( pd.products_id = p2c.products_id )
LEFT JOIN products p ON ( p.products_id = p2c.products_id )
WHERE p.products_status =1
AND pd.language_id = '1'
AND (o.date_purchased >= '2010-03-12'
AND o.date_purchased <= '2010-05-13')
AND p2c.categories_id
IN ( 8, 6, 10, 11, 12, 4, 7, 9, 13, 5 )
GROUP BY p2c.products_id
ORDER BY products_ordered DESC , products_name ASC;
The issue I am having is that if a product is linked, it doubles up the products_ordered. Any fresh eyes see the mistake?
If anyone was wondering how to do this, the following query worked:
PHP Code:
SELECT DISTINCT (p2c.products_id), SUM( op.products_quantity ) AS products_ordered, pd.products_name, p.products_image
FROM products_to_categories p2c
LEFT JOIN orders_products op ON ( op.products_id = p2c.products_id )
LEFT JOIN orders o ON ( o.orders_id = op.orders_id )
LEFT JOIN products_description pd ON ( pd.products_id = p2c.products_id )
LEFT JOIN products p ON ( p.products_id = p2c.products_id )
WHERE p.products_status =1
AND pd.language_id = '1'
AND (o.date_purchased >= '2010-03-12'
AND o.date_purchased <= '2010-05-13')
AND p2c.categories_id
IN ( 8, 6, 10, 11, 12, 4, 7, 9, 13, 5 )
GROUP BY p2c.products_id, p2c.categories_id
ORDER BY products_ordered DESC , products_name ASC;
Bookmarks