如何在PHP SELECT语句中读取SUM()分组函数?

时间:2010-02-16 11:40:15

标签: php mysql

我有这个

"SELECT SUM( state ) FROM `vote` WHERE id ='5'"

现在在php中添加它时,我该怎么做

$xx= mysql_fetch_array(
    mysql_query("SELECT SUM( state ) FROM `vote` WHERE id ='5'")
);

我能这样做吗:

echo $xx['SUM( state )'];

3 个答案:

答案 0 :(得分:8)

给SUM(州)一个别名。

SELECT SUM( state ) AS SummedState FROM `vote` WHERE id ='5'

现在,引用别名

echo $xx['SummedState']

答案 1 :(得分:1)

您需要在SQL查询中使用别名:

"SELECT SUM( state ) as result FROM `vote` WHEREid ='5'"

请注意我在查询中添加的“as column_name”。


然后,您将能够从PHP代码访问“结果”列:

echo $xx['result'];

答案 2 :(得分:1)

您可以使用SUM(状态)的别名:

"SELECT SUM(state) AS state_sum FROM `vote` WHERE id ='5'"

然后你可以按如下方式引用它:

echo $xx['state_sum']