これは、 を乗じINNER JOIN
た集合体を持つ のSUM()
比較的単純なシーケンスです。quantity
paid_price
SELECT
p.name,
/* aggregate SUM() of orders_products qty * price */
SUM(op.quantity * op.paid_price) AS total_sale_price
FROM
products
/* Join through orders_products (which gives the quantity) */
INNER JOIN orders_products op ON p.id = op.products_id
/* ...to orders (which provides the date) */
INNER JOIN orders o ON op.orders_id = o.id
/* The last week... */
WHERE o.purchased_at >= (CURDATE() - INTERVAL 7 DAY)
GROUP BY p.name
/* In descending order of quantity, only the top 5 */
ORDER BY total_paid_price DESC LIMIT 5
時間をかけて集計関数を確認することをお勧めします。