2010-08-04 107 views
2

我對mysql查詢有些困惑。任何幫助讚賞。以特定時間間隔對行進行MySQL查詢

我當前的查詢:

SELECT 
    r.name 
, r.network 
, i.name 
, d.dtime 
, d.input 
FROM router AS r 
INNER JOIN interface AS i 
ON r.rid = i.rid 
INNER JOIN 1273118400_1_60 AS d 
ON i.id = d.id 
AND dtime BETWEEN 1273152000 AND 1273153800 WHERE i.status = "active" 

現在我使用UNIX_TIMESTAMP爲這間我查詢的時間間隔。 現在我得到的結果爲: 1273152000,1273152001,1273152002等。

我想要間隔60秒,如: 1273152000,1273152060,1273152120等。

任何想法我怎麼能去呢?

請幫忙。 謝謝

Jillika

回答

2

我不知道這是否可以進一步簡化,但你可能想嘗試以下操作:

SELECT  r.name, 
      r.network, 
      i.name, 
      d.dtime, 
      d.input 
FROM   router AS r 
INNER JOIN interface AS i ON (r.rid = i.rid) 
INNER JOIN (
       SELECT  t.dtime, dt.input, dt.id 
       FROM  1273118400_1_60 AS t 
       INNER JOIN (
           SELECT dtime, id, input 
           FROM 1273118400_1_60 
          ) AS dt ON (dt.dtime = t.dtime AND dt.id = t.id) 
       GROUP BY t.dtime DIV 60 
      ) AS d ON (i.id = d.id) 
WHERE  (d.dtime BETWEEN 1273152000 AND 1273153800) AND 
      (i.status = 'active'); 

測試用例:

CREATE TABLE router (rid int, name varchar(20), network int); 
CREATE TABLE interface (id int, rid int, status varchar(20), name varchar(10)); 
CREATE TABLE 1273118400_1_60 (id int, dtime int, input int); 

INSERT INTO router VALUES (1, 'router A', 0); 
INSERT INTO interface VALUES (1, 1, 'active', 'interface A'); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273151999, 1); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152000, 2); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152001, 3); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152002, 4); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152059, 5); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152060, 6); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152061, 7); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152062, 8); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152119, 9); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152120, 10); 
INSERT INTO 1273118400_1_60 VALUES (1, 1273152121, 11); 

結果:

+----------+---------+-------------+------------+-------+ 
| name  | network | name  | dtime  | input | 
+----------+---------+-------------+------------+-------+ 
| router A |  0 | interface A | 1273152000 |  2 | 
| router A |  0 | interface A | 1273152060 |  6 | 
| router A |  0 | interface A | 1273152120 | 10 | 
+----------+---------+-------------+------------+-------+ 
3 rows in set (0.00 sec) 
0

你的問題是不是太清楚,但無論如何試試這個:

SELECT r.name, r.network, i.name, d.dtime, d.input 
FROM router r 
INNER JOIN interface i 
ON r.rid = i.rid 
INNER JOIN 1273118400_1_60 AS d 
ON i.id = d.id 
WHERE d.time > 1273152000 AND d.time < 1273153800 
AND i.status = "active"; 
+0

嗨, 感謝您的快速反應。 將嘗試使其更清晰: 1273152000 = unixtimestamp爲:05/07/2010 09:20 1273153800 = unixtimestamp爲:05/07/2010 09:50 我想查詢每分鐘間隔(60秒)因爲我的表格包含每秒鐘的值。 dtime = unix_timestamp中的時間。 任何想法我怎麼能做到這一點? – JJunior 2010-08-04 15:13:06

+0

嗯,我會考慮創建一個存儲函數來處理它,如果它比這更復雜。 – Evernoob 2010-08-04 15:32:29

0
SELECT 
r.name 
,r.network 
,i.name 
, d.dtime 
, 60*(floor(d.dtime/60)) dtime2 
, d.input 
FROM router AS r 
INNER JOIN interface AS i 
ON r.rid = i.rid 
INNER JOIN 1273118400_1_60 AS d 
ON i.id = d.id 
AND dtime BETWEEN 1273152000 AND 1273153800 WHERE i.status = "active"