2014-09-26 67 views
0

我試圖獲得出現一對產品的所有訂單。簡單的Mysql查詢。 2條件

我有一個表像id_order id_line(鍵)id_product

我使用 SELECT COUNT(DISTINCT id_order) FROM table WHERE id_product = i知道在多少訂單出現一個產品,但我不知道如何提出一個對他們的。

謝謝大家提前,

我希望這是明確的:)

回答

1

我猜你有第二個表,訂單行

SELECT id_product, id_order FROM order_table AS ot 
INNER JOIN product_table AS pt1 ON ot.id_order=pt1.id_order AND pt1.id_product=i 
INNER JOIN product_table AS pt2 ON ot.id_order=pt2.id_order AND pt2.id_product=j 
0
select id_product, count(distinct id_order) 
from table 
where id_product in (i, j) 
group by id_product 
+0

謝謝你的評論,但不幸的是這不起作用:( – 2014-09-26 12:55:42

+1

當提供示例代碼或SQL請添加解釋 – 2014-09-26 13:23:47