gpt4 book ai didi

sql - MySQL查询where条件问题

转载 作者:行者123 更新时间:2023-11-29 05:43:50 26 4
gpt4 key购买 nike

我有一个工作查询作为

   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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com