【发布时间】:2026-02-02 21:20:04
【问题描述】:
我有这样的查询
SELECT o.product_id,
p.name,
count(o.product_id) AS total_products_sold,
(SELECT count(o.id)
FROM ebay.`order` o) AS total_orders
FROM ebay.`order` o
INNER JOIN product p ON o.product_id = p.id
GROUP BY o.product_id
total_orders 在为每个我不想要的东西执行时重新运行。我
问题:
我希望 total_orders 与外部查询的每个结果集相结合。
我试过了,但它只返回 1 行
SELECT tps.product_id,
tps.name,
tps.total_products_sold,
count(oo.id) AS total_orders
FROM ebay.`order` oo
INNER JOIN
( SELECT o.id,
o.product_id,
p.name,
count(o.product_id) AS total_products_sold
FROM ebay.`order` o
INNER JOIN product p ON o.product_id = p.id
GROUP BY o.product_id ) AS tps ON oo.product_id = tps.product_id
有更好的解决方案吗?
谢谢。
【问题讨论】:
-
你能给出你想要的结果的样本记录吗?