有比我更快的解决方案吗?
我有一个包含 orders_id 和 products_id 列的表(无法更改)
现在我想知道有多少订单使用唯一 products_id。
我尝试了以下。但是它在加载太多行(160'000)时崩溃。
SELECT DISTINCT `products_id` , `products_name` , COUNT(*) as totalorders FROM `orders_products` ORDER BY `products_id` ASC
我也尝试了子查询,但问题与上述相同。
SELECT (SELECT count(*) FROM orders_products WHERE products_id = op.products_id) AS totalorders, products_id, products_name FROM orders_products op ORDER BY products_id ASC