+-------------+---------------+
| order_no | product_id |
| 55 | 765 |
| 55 | 564 |
| 34 | 323 |
| 42 | 223 |
+-------------+---------------+
我想知道一个查询,我可以使用“765”作为product_id来获取order_no的总数。
我的意思是说我希望输出为“2”,因为order_no“55”有两个产品。所以,如果我在查询中输入“765”,我会得到“2”。
答案 0 :(得分:0)
http://sqlfiddle.com/#!9/eff29/4
SELECT
t.product_id,
t.order_no,
COUNT(tj.product_id)
FROM t1 t
LEFT JOIN t1 tj
ON t.order_no = tj.order_no
WHERE t.product_id=765
GROUP BY t.product_id, t.order_no
答案 1 :(得分:0)
试试这个。你可以给一个products_id:
SELECT COUNT(*), order_no FROM youTable
WHERE order_no IN (
SELECT order_no FROM yourTable WHERE product_id = 765
)
GROUP BY order_no;