开发者

MySQL query where condition problem

I have a working query as

   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

Now, I need only those records where either sales is not equal to 0 or开发者_如何学C links is not equal to 0 or both are not equal to 0

How can I achieve this?


Add a HAVING clause

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜