2010-05-22 20 views
4

我需要顯示來自數據庫的即將發生的事件。問題是當我使用查詢我目前正在使用任何事件與已開始的一天已經通過將在即將到來的事件列表中顯示較低的事實,無論它們是當前的事實SQL查詢選擇即將開始和結束日期的事件

我的表(yaml):

columns: 
    title: 
     type: string(255) 
     notnull: true 
     default: Untitled Event 
    start_time: 
     type: time 
    end_time: 
     type: time 
    start_day: 
     type: date 
     notnull: true 
    end_day: 
     type: date 
    description: 
     type: string(500) 
     default: This event has no description 
    category_id: integer 

我的查詢(教義):

$results = Doctrine_Query::create() 
     ->from("sfEventItem e, e.Category c") 
     ->select("e.title, e.start_day, e.description, e.category_id, e.slug") 
     ->addSelect("c.title, c.slug") 
     ->orderBy("e.start_day, e.start_time, e.title") 
     ->limit(5) 
     ->execute(array(), Doctrine_Core::HYDRATE_ARRAY); 

基本上我想當前正在進行的任何事件(所以如果今天是在START_DAY和END_DAY之間)是在的頂部名單。如果可能的話,我會怎麼做?原始的sql查詢也是很好的答案,因爲它們很容易變成DQL。

SOLUTION 基於下面的答案之一:

$results = Doctrine_Query::create() 
     ->from("sfEventItem e, e.Category c") 
     ->select("e.title, e.start_day, e.description, e.category_id, e.slug") 
     ->addSelect("c.title, c.slug") 
     ->addSelect("IF(NOW() BETWEEN e.start_day AND e.end_day,0,1) as score") 
     ->orderBy("score, e.start_day, e.start_time, e.title") 
     ->limit(5) 
     ->execute(array(), Doctrine_Core::HYDRATE_ARRAY); 

回答

3

這應該這樣做:

->orderBy("IF(NOW() BETWEEN e.start_day AND e.end_day,0,1) , e.start_day, e.start_time, e.title") 
+0

這很漂亮,它修改了一點(因爲某些原因,原則停止選擇任何記錄時,它在orderBy) – 2010-05-22 17:31:51

2

您可以使用case

添加的情況下,噹噹前日期是開始和結束日期之間,比分1 所有的人有得分0

然後才能按分數。

+0

謝謝我不知道MySQL有這樣的。不幸的是,學說不支持(我們爲了方便而付出的代價)+1 – 2010-05-22 17:28:23

相關問題