简体   繁体   English

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

[英]Select last record from same day

Heres the problem... I have a loop looking like this selecting stuff from my database. 继承人的问题...我有一个循环,就像这样从我的数据库中选择东西。

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

This code outputs the data for each row and separates it for year month day etc. However some of the data is on the same day. 此代码输出每一行的数据,并将其分隔为年月日等。但是,某些数据在同一天。 Thats fine BUT when data is on the same day i want a variable that holds only the last of the items on that same day. 很好,但是当数据在同一天时,我想要一个变量,该变量仅保留同一天的最后一项。

EX. 例如

DB D B

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

The result I would like from this is something like this 我想要的结果是这样的

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

Meaning test 1 in this case holds information about test 2 since test 2 is after test 1 and they both have the same date. 在这种情况下,测试1包含测试2的信息,因为测试2在测试1之后,并且两者的日期相同。 If there is only one on the specific date as test 2 and 3 just echo the same id as the variable. 如果在特定日期只有一个,如测试2和3,则回显与变量相同的ID。

Any ideas? 有任何想法吗? PLease tell me if i need to explain this even more :) 请告诉我是否需要进一步解释一下:)

Thanks a lot for any help! 非常感谢您的帮助!

Group by date(begin)as you want only 1 record on the same date(begin) and since you want the latest order by dsc on column 'begin' (I believe the 'begin' is stored as datetime else you need to have dsc on table.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