选择MAX(ad_number)问题的查询

时间:2014-09-10 17:56:37

标签: mysql select max

$r = ca_mysql_query("SELECT MAX(ad_number) FROM `myTable` WHERE  `user` = {$user_id} ");

$max = $r[0]['ad_number'];
echo $max;

它应该打印max ad_number但它没有返回任何值
wheares, 如果我尝试查询从数组中删除MAX(ad_number)其returnig字段数据。

$r = ca_mysql_query("SELECT * FROM `myTable` WHERE  `user` = {$user_id} ");

$max = $r[0]['ad_number'];

返回给定记录的值

什么是错的?

2 个答案:

答案 0 :(得分:0)

$r = ca_mysql_query("SELECT MAX(ad_number) AS AD_NUMBER FROM myTable WHERE user = {$user_id} ");

$max = $r[0]['AD_NUMBER']; 
echo $max;

答案 1 :(得分:0)

为您使用别名列:

SELECT MAX(ad_number) as max_ad_number FROM myTable WHERE  user = {$user_id}

然后这样做:

$max = $r[0]['max_ad_number']; echo $max;

请参阅w3schools here

中的文档