从数据库分组日期获取

时间:2012-10-09 07:04:40

标签: php mysql

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

$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>';
            }

        }

有许多行具有相同的fixed_date。

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

现在我想创建具有相同fixed_date的行的组。

请帮帮我。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果不了解您的牌桌,我们会进行一定程度的猜测。关系,但尝试一下:

SELECT cd。*,cp.party_id,cp.advo_id,cp.MaxDate 从 (     SELECT party_id,advo_id,MAX(fixed_date)AS MaxDate     来自case_proceeding     GROUP BY party_id )cp INNER JOIN case_decision cd ON cp.party_id = cd.party_id 按顺序cp.MaxDate