2010-12-13 123 views
1

我有一個工作查詢作爲MySQL查詢的WHERE條件問題

SELECT p.id, 
      p.name AS ProductName, 
      count(DISTINcT s.salesid) as Sales, 
      Count(DISTINCT l.linkid) as Links 
    FROM products p 
LEFT JOIN sales s ON p.id=s.productid 
LEFT JOIN links l ON p.id=l.productid 
GROUP BY p.id 

現在,我只需要那些記錄,或者銷售不等於0或鏈接不等於0或兩個不等於0

我該如何做到這一點?

回答

3

添加HAVING子句

SELECT p.id, p.name AS ProductName, 
count(DISTINcT s.salesid) as Sales, Count(DISTINCT l.linkid) as Links 
FROM products p 
LEFT JOIN sales s ON p.id=s.productid 
LEFT JOIN links l ON p.id=l.productid 
GROUP BY p.id 
HAVING Sales > 0 OR Links > 0 
+0

它的工作原理,謝謝 – 2010-12-13 05:50:07