gpt4 book ai didi

mysql - 如何在 MySQL 中不显示负数?

转载 作者:行者123 更新时间:2023-12-01 00:45:01 25 4
gpt4 key购买 nike

我有这段代码(有效):

SELECT sportsID, newsID, cID, SUM(likes) AS totalLikes 
FROM like_table
GROUP BY sportsID, newsID
ORDER BY totalLikes DESC

我试图修改它以不显示负值,但它不起作用。

SELECT sportsID, newsID, cID, SUM(likes) AS totalLikes 
FROM like_table
WHERE totalLikes > 0
GROUP BY sportsID, newsID
ORDER BY totalLikes DESC

有人可以帮忙吗?我不知道我做错了什么。

最佳答案

尝试使用 HAVING 子句:

SELECT sportsID, newsID, cID, SUM(likes) AS totalLikes 
FROM like_table
GROUP BY sportsID, newsID
HAVING SUM(likes)> 0
ORDER BY totalLikes DESC

解释:

HAVING 子句被添加到 SQL 中,因为 WHERE 关键字不能与聚合函数一起使用。

WHERE 子句对所选列设置条件,而 HAVING 子句对 GROUP BY 子句创建的组设置条件。

关于mysql - 如何在 MySQL 中不显示负数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23446607/

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