[英]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 having to use the while loop? 是否有更简洁的方法,可能在查询本身而不必使用while循环?
You can use the MySQL sum function and get the sum from MySQL itself: 您可以使用MySQL sum函数从MySQL本身获取总和:
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. 获取查询在
$row
生成的单行, $row['vote_count_sum']
将具有总计。
SELECT SUM( vote_count ) AS vote_summary FROM votes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.