2011-07-30 42 views
0

嗨,我有一個表演(表演)表,我希望能夠從今天(getdate())選擇前一個和下一個「顯示」)如何根據日期從表中選擇上一個和下一個事件

表結構有這個; SHOW_ID,ShowNumber,Name,EventTime

SELECT SHOW_ID, ShowNumber, Name, EventTime 
FROM Event Where EventID = @EventID 

這就是我卡住的地方,我如何去做這件事,在此先感謝。

+0

你能澄清一下你是否需要用@EventID做些事情,或者與這個問題無關嗎? –

+0

Yestid是重要的,因爲我正在做一個基於特定事件的臉。 – pmillio

+0

所以你想要事件之前和之後的@EventID,或者你想要今天之前和之後的事件(getdate())。你的問題似乎矛盾。 –

回答

1
-- Get the next showing of the event that will occur directly after the current datetime 
SELECT TOP 1 SHOW_ID, ShowNumber, Name, EventTime 
FROM Event WHERE EventTime > GetDate() AND EventId = @EventId 
ORDER BY EventTime asc 

-- Optionally, if you wanted to get the above and below results in a single SELECT, 
-- you could use a UNION here. i.e.: 
-- UNION 

-- Get the first event that occurred directly before the current datetime 
SELECT TOP 1 SHOW_ID, ShowNumber, Name, EventTime 
FROM Event WHERE EventTime < GetDate() AND EventId = @EventId 
ORDER BY EventTime desc 
相關問題