开发者

How to read a SUM() grouping function in a PHP SELECT statement?

I have this

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

now when add it in php, how i can do

$xx开发者_如何学C= mysql_fetch_array(
    mysql_query("SELECT SUM( state ) FROM `vote` WHERE id ='5'")
);

Can I can do it like that:

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


Give SUM( state ) an alias.

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

Now, reference the alias

echo $xx['SummedState']


You need to use an alias in your SQL query :

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

Note the "as column_name" I added in the query.


And, then, you'll be able to access the "result" column from your PHP code :

echo $xx['result'];


You can use an alias for SUM(state):

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

Then you would be able to reference it as follows:

echo $xx['state_sum']
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜