[英]Mysql query using where and group by clause
I have the following table. 我有下表。
mysql> select * from consumer9;
+------------+--------------+-------------------+
| Service_ID | Service_Type | consumer_feedback |
+------------+--------------+-------------------+
| 100 | Computing | -1 |
| 35 | Printer | 0 |
| 73 | Computing | -1 |
| 50 | Data | 0 |
+------------+--------------+-------------------+
I want to use GROUP BY
clause in my project. 我想在项目中使用GROUP BY
子句。 I am getting an error when I am using the query: 使用查询时出现错误:
SELECT Service_ID, Service_Type, SUM(consumer_feedback)
FROM consumer9
GROUP BY Service_ID
WHERE Service_Type=Printer;
Error 错误
ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where Service_Type=Printer' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ where Service_Type = Printer”附近使用
The following query should work. 以下查询应该工作。
select Service_ID, Service_Type, sum(consumer_feedback)
from consumer9
where Service_Type=Printer
group by Service_ID, Service_Type;
Remember, the where clause goes before the group by clause and all non-aggregated terms in the select part will have to be present in the group by clause. 请记住,where子句位于group by子句之前,并且select部分中所有未聚合的术语都必须出现在group by子句中。
Use the having
clause instead of where
使用having
子句代替where
SELECT Service_ID, Service_Type, SUM(consumer_feedback)
FROM consumer9
GROUP BY Service_ID
HAVING Service_Type='Printer';
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.