从同一天选择最后一条记录

时间:2011-05-22 10:42:12

标签: php mysql date select

继承人的问题...... 我有一个循环看起来像从我的数据库中选择的东西。

$query = "SELECT * FROM table WHERE publish <= CURDATE() AND active = 'Yes' AND (YEAR(begin) = YEAR(CURDATE()) OR YEAR(begin) = YEAR(CURDATE()) + 1) ORDER BY begin ASC";
                        $resultSet = mysql_query($query);

                        if (mysql_num_rows($resultSet))
                        {
                        $newsArray = array();


                        while ($newsResult = mysql_fetch_array($resultSet))
                        {                                   
                        $newDate =  $newsResult['begin'] ;   
                        $timePeriod = date('F  Y ',strtotime($newDate));

                        $timePeriodY = date('Y',strtotime($timePeriod));
                        $timePeriodM = date('F',strtotime($timePeriod));                            

                        if (!isset($newsArray[$timePeriod]))
                        {
                        $newsArray[$timePeriod] = array();
                        }       
                        $newsArray[$timePeriod][] = $newsResult;                            
                        }                        


                        foreach ($newsArray as $timePeriod => $newsItems)
                        {  

                        $timePeriodY = date('Y',strtotime($timePeriod));

                        if ($timePeriodY == $thisYear){


                            }
                        else if ($timePeriodY == $nextYear){


                        }

                        echo '<h2 class="year>'.$timePeriodY.'</h2>';
                        echo '<ul class="column">';

                        foreach ($newsArray as $timePeriod => $newsItems)
                        { 
                        $timePeriodMonth = date('Y',strtotime($timePeriod));
                        if ($timePeriodY == $timePeriodMonth) {


                        $moSe = strftime('%B',strtotime($timePeriod));
                        $MonthSe = ucfirst($moSe);


                        $seMonth = str_replace($dateSearch,$dateReplace,date('F',strtotime($timePeriod)));

                        echo $seMonth;                     

                        foreach ($newsItems as $item)
                        {



                        $ad_event = date("Y-m-d",strtotime($item['event_end']));
                        $today = date("Y-m-d");




                            $dayMod = strftime('%e',strtotime($item['event_begin']));

                            $seDay = str_replace($dateSearch,$dateReplace,date('D',strtotime($item['event_begin'])));




                        echo '<a href="javascript:gotoDotBottom('.$item['event_id'].',\''.$item['event_url'].'\');" id="e'.$item['event_id'].'" class="eventLink '.$markedEvent.'" overhead="'.$item['event_title'].'" overdate="'.date("Y-m-d",strtotime($item['event_begin'])).'" overtext="'. strip_tags($showSum) .'">'.$seDay.' '.$dayMod.' - '.$item['event_title'].'</a>';
                        echo '</div>';
                        }  



                                    }            
                            }

                                                   }


                        }   

                        else
                            {
                        echo '';
                        }

此代码输出每行的数据,并将其分为年月等。但有些数据是在同一天。这很好但是当数据在同一天我想要一个只包含同一天最后一个项目的变量。

EX。

DB

PUBLISH      ID    NAME
2011-05-05   1     test 1
2011-05-05   2     test 2
2011-05-06   3     test 3

我想从中得到的结果是这样的

RESULT

test 1 ID 1 PUBLISH 2011-05-05 VARIABLE 2 <<-- see the variable that comes from test 2 row
test 2 ID 2 PUBLISH 2011-05-05 VARIABLE 2
test 3 ID 3 PUBLISH 2011-05-06 VARIABLE 3

在这种情况下,含义测试1保存有关测试2的信息,因为测试2在测试1之后,它们都具有相同的日期。如果在特定日期只有一个作为测试2和3,则只回显与变量相同的id。

有什么想法吗?请告诉我是否需要更多地解释这个:)

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

按日期(开始)分组,因为您希望在同一日期(开始)只有1条记录,并且因为您希望dsc在列'begin'上的最新订单(我相信'begin'存储为datetime,否则您需要在桌面上有dsc.ID)

$query = "SELECT * 
               FROM table 
               WHERE publish <= CURDATE() 
                AND active = 'Yes' 
                  AND (YEAR(begin) = YEAR(CURDATE()) 
                  OR YEAR(begin) = YEAR(CURDATE()) + 1) 
                 GROUP BY  begin
                 ORDER BY begin DSC";