简体   繁体   English

MySQL COUNT查询返回行中值的总和

[英]MySQL COUNT query return total of values in rows

I have a guestbook, with messages stored in a table. 我有一个留言簿,消息存储在表格中。 There is a 'hits' field for each message. 每条消息都有一个“点击”字段。 How do I run a query that will count the total number of hits? 如何运行将计算总点击次数的查询?

SELECT name, COUNT(hits) FROM guestbook_message WHERE name='".$req_user_info['username']."' GROUP BY name";

This returns the amount of messages that the user has posted, where there is a value in the 'hits' field. 这将返回用户已发布的消息量,其中“点击”字段中有值。 But not the total amount of hits. 但不是命中总数。

If there are 3 messages, with 3 hits each, it should return "9 hits". 如果有3条消息,每条消息有3次点击,则应返回“9次点击”。 But the query I posted above would return "3". 但我上面发布的查询将返回“3”。

Many thanks. 非常感谢。

我想你是在追求SUM

SELECT name, SUM(hits) FROM guestbook_message WHERE name='".$req_user_info['username']."' GROUP BY name";

您想要SUM而不是COUNT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM