从mysql中的表中获取最大值

时间:2012-11-28 06:20:21

标签: php mysql sql

我想从按party_id分组的表中获取或获取最大日期。

要做到这一点,我试过:

$check = mysql_fetch_array(mysql_query("select max(fixed_date) as f from case_proceeding group by party_id where party_id='$row[party_id]'"));

之后,我想打印这样的最大日期:

echo $check['f'];

但它不起作用。我认为mysql_query中有一个错误。

4 个答案:

答案 0 :(得分:0)

尝试,因为您已经从GROUP BY过滤了<{1}}

,因此此处不需要WHERE应该在WHERE子句之后
$check = mysql_fetch_array(mysql_query("select max(fixed_date) as f from case_proceeding where party_id='$row[$party_id]'"));

SQL Injection您的问题很容易受到攻击,请阅读以下文章以防范它

答案 1 :(得分:0)

试试这个:

此处不需要GROUP BY子句

select max(fixed_date) as f 
from   case_proceeding 
where  party_id='$row[party_id]

答案 2 :(得分:0)

试试这个::

select max(DATE(fixed_date)) as f from case_proceeding where party_id=? group by party_id 

答案 3 :(得分:0)

$check = mysql_fetch_array(mysql_query("select max(fixed_date) as f from case_proceeding where party_id='$row[party_id]' "));

注意:不推荐使用mysql_ *函数,所以请尝试使用pdo或mysqli