开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜