1

我正在使用此查询从数据库中选择行

$date=mysql_query("SELECT party_id, advo_id, max(fixed_date) as d FROM `case_proceeding` group by party_id order by fixed_date");

    while($drow=mysql_fetch_array($date)){  

    if($drow['d']<$today){

        $decide=mysql_query("select * from case_decision where party_id='$drow[party_id]'");

        if(mysql_num_rows($decide)=='0')
        {

            if($drow['d']!='0000-00-00')
            {
                echo '<a href=case-tracking.php?date='.$drow['d'].'&party_id='.$drow['party_id'].'&advo_id='.$drow['advo_id'].' style=color:#000000;>'.date('d/m/Y',strtotime($drow['d'])).'</a>';
            }

        }

有许多行具有相同的固定日期。

例如。party_id=1 有 fixed_date=2012-08-12 和 party_id=5 也有相同的日期 fixed_date=2012-08-12

现在我想将具有相同固定日期的行分组。

请帮我。

4

2 回答 2

0

尝试这个:

SELECT t1.party_id, t2.advo_id, t1.MaxDate
FROM
(
    SELECT party_id, 
      MAX(fixed_date) AS MaxDate 
    FROM `case_proceeding` 
    GROUP BY party_id 
) t1
INNER JOIN case_proceeding t2  ON t1.party_id = t2.party_id 
                              AND t1.MaxDate  = t2.fixed_date
ORDER BY fixed_date
于 2012-10-09T07:12:37.137 回答
0

在不知道您的表格和关系的情况下涉及一定数量的猜测,但请尝试一下:

SELECT cd.*, cp.party_id, cp.advo_id, cp.MaxDate FROM (SELECT party_id, advo_id, MAX(fixed_date) AS MaxDate FROM case_proceeding GROUP BY party_id) cp INNER JOIN case_decision cd ON cp.party_id = cd.party_id ORDER BY cp .MaxDate

于 2012-10-09T15:03:59.280 回答