MySQL以特定时间间隔查询行

时间:2010-08-04 14:49:04

标签: sql database 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

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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";

答案 2 :(得分: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"