MySQL查询使用where和group by子句 [英] Mysql query using where and group by clause

查看:1154
本文介绍了MySQL查询使用where和group by子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表.

mysql> select * from consumer9;
+------------+--------------+-------------------+
| Service_ID | Service_Type | consumer_feedback |
+------------+--------------+-------------------+
|        100 | Computing    |                -1 |
|         35 | Printer      |                 0 |
|         73 | Computing    |                -1 |
|         50 | Data         |                 0 |
+------------+--------------+-------------------+

我想在我的项目中使用GROUP BY子句.使用查询时出现错误:

I want to use GROUP BY clause in my project. 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;

错误

错误1064(42000):您的SQL语法有错误;检查 与您的MySQL服务器版本相对应的手册 在第1行的"where Service_Type = Printer"附近使用的语法

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where Service_Type=Printer' at line 1

推荐答案

以下查询应该有效.

select Service_ID, Service_Type, sum(consumer_feedback) 
from consumer9 
where Service_Type=Printer
group by Service_ID, Service_Type;

请记住,where子句位于group by子句之前,并且select部分中所有未聚合的术语都必须出现在group by子句中.

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.

这篇关于MySQL查询使用where和group by子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆