简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM