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
精彩评论