MySQL查询条件问题

时间:2010-12-13 05:43:33

标签: sql mysql

我的工作查询为

   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的记录

我怎样才能做到这一点?

1 个答案:

答案 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