我有一个工作查询作为
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 的记录
我怎样才能做到这一点?
最佳答案
添加一个 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
关于sql - MySQL查询where条件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4426116/