2011-12-11 32 views
0

首先,即時使用SQL將數據隔離到3個不同的時間(24小時)框架。只有SQL查詢語言。我正在使用MYSQL工作臺來提取我的項目所需的一些功能。我設法提取所需的功能,並將其存儲在新的表格中調用新事件。我也將時間轉換爲整數。以下是所使用的SQL代碼: -如何使用MYSQL

select iphdr.cid ,iphdr.ip_dst, date(event.timestamp), convert(time(event.timestamp), unsigned) as newtime, event.signature 
from iphdr, event 
where iphdr.cid = event.cid 
order by event.signature; 

結果(某些輸出的)

 
cid |ip_add  |date  |newtime|sig. 

480 | 3232284675 | 2011-11-19 |4328 | 1 
482 | 3232284675 | 2011-11-19 |4328 | 1 
1 | 3232284675 | 2011-11-19 |113928 | 1 
2 | 3232284675 | 2011-11-19 |235959 | 2 

由於我已經轉換的時間爲數字;因此,新時間,現在我需要在24小時內(時間框架0,1和2)將上述查詢的輸出分隔爲三個不同的時間範圍。這是問題....當我嘗試使用代碼(下面)時,我不斷收到1064錯誤代碼。我認爲通過使用IF然後Else,我可以很容易地將數據分成3個不同的時間範圍。 我需要的輸出就像下面

 
cid |ip_add  |date  |newtime|sig. |time Frame 
480 | 3232284675 | 2011-11-19 | 4328 | 1 | 0 
482 | 3232284675 | 2011-11-19 |4328 | 1 | 0 
1 | 3232284675 | 2011-11-19 |113928 | 1 | 1 
2 | 3232284675 | 2011-11-19 |235959 | 2 | 2 

下面是我所用想做到這一點的代碼,但是我不斷收到錯誤代碼1064

select newtime from new_event for every row 
IF newtime < 80101 
    than newtime = 0 
else if newtime > 80000 and newtime < 160101 than newtime = 1 
else if newtime >160000 than newtime = 2 
end if 

我希望IM做更有意義。當談到編程時,我是一個完整的klutz。對不起 。我也試圖通過瀏覽網絡使這項工作。在等待你的回覆時,我會努力工作。

回答

1
if (newtime>160000, 2, if(newtime>80000, 1, 0)) 

我以前誤解,如果你不希望打入8小時框架,
上面應該幫助

+0

謝謝你,所以非常。我終於明白了。我現在可以去睡覺了。 – user1092074

+0

不,這是錯誤的語法,只是從new_event選擇if(newtime> 160000,2,if(newtime> 80000,1,0))作爲time_frame;' – ajreal

0

你可以子查詢中附上您的查詢,如果您仍然需要NEWTIME。 (只是僞代碼,請使用適當的語法到數據庫)

select *, newtime/3 as segment 
FROM "your-query" 

在mysql中考慮使用TIME

SELECT TIME(event.timestamp)/3 as seqment