第一:是的,我是一个 sql 菜鸟,我真的需要一些帮助:
SELECT COUNT(id), SUM(rating), COUNT(rating) FROM comments WHERE phone='$phone';"
这需要是这样的:
SELECT COUNT(id), SUM(rating), (COUNT(rating) WHERE rating>0) FROM comments WHERE phone='$phone';"
任何人都可以帮忙吗?
所以你基本上想要这个?
SELECT
COUNT(id),
SUM(rating),
COUNT(rating)
FROM comments
WHERE phone='$phone' AND rating > 0
如果没有,请在您的帖子中添加一些解释。
Ps 我猜你正在使用 PHP。确保您不会陷入称为 MySQL 注入的陷阱
您可以在 COUNT 中使用 CASE 语句:-
$query = "SELECT COUNT(id), SUM(rating), (SUM(CASE WHEN rating > 0 THEN 1 ELSE 0 END))
FROM comments
WHERE phone='$phone'";