我不知道這是否可以進一步簡化,但你可能想嘗試以下操作:
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)
嗨, 感謝您的快速反應。 將嘗試使其更清晰: 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
嗯,我會考慮創建一個存儲函數來處理它,如果它比這更復雜。 – Evernoob 2010-08-04 15:32:29