簡體   English   中英

選擇同一天的最后一條記錄

[英]Select last record from same day

繼承人的問題...我有一個循環,就像這樣從我的數據庫中選擇東西。

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

此代碼輸出每一行的數據,並將其分隔為年月日等。但是,某些數據在同一天。 很好,但是當數據在同一天時,我想要一個變量,該變量僅保留同一天的最后一項。

例如

D B

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

我想要的結果是這樣的

結果

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條記錄,並且由於您希望按dsc在“開始”列上進行最新排序(我相信“開始”存儲為日期時間,否則您需要使用dsc在table.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";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM