开发者

How to SELECT the total values from a column in MySQL?

开发者 https://www.devze.com 2023-02-16 17:28 出处:网络
Here\'s what I\'m currently using: $query = mysql_query(\" SELECT vote_count FROM votes \"); while ($row = mysql_fetch_array($query)) {

Here's what I'm currently using:

$query = mysql_query(" SELECT vote_count FROM votes ");
while ($row = mysql_fetch_array($query)) {    
    $total_votes += $row['vote_count'];
}

echo $total_votes;

Is there a more concise way, perhaps in the query itself without h开发者_如何学Goaving to use the while loop?


You can use the MySQL sum function and get the sum from MySQL itself:

SELECT sum(vote_count) AS vote_count_sum FROM votes

Fetch the single row that the query produces in $row and $row['vote_count_sum'] will have the total.


SELECT SUM( vote_count ) AS vote_summary FROM votes 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号