简体   繁体   中英

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". But the query I posted above would return "3".

Many thanks.

我想你是在追求SUM

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

您想要SUM而不是COUNT。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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