2014-12-26 55 views
1

我有以下數據的表:MySQL的相同查詢的不同標準

+----------+---------------------+ 
| sesiones | fecha    | 
+----------+---------------------+ 
|  350 | 2014-12-26 12:49:02 | 
|  329 | 2014-12-26 12:48:01 | 
|  320 | 2014-12-26 12:47:02 | 
|  320 | 2014-12-26 12:46:01 | 
|  328 | 2014-12-26 12:45:02 | 
|  314 | 2014-12-26 12:44:01 | 
|  318 | 2014-12-26 12:43:02 | 
|  308 | 2014-12-26 12:42:01 | 
|  326 | 2014-12-26 12:41:02 | 
|  308 | 2014-12-26 12:40:01 | 

我需要提取「sesiones」在同一查詢,與插入數據的日期時間,用「sesiones另一列「前一分鐘,但保持日期。

輸出將是:

+----------+--------+---------------------+ 
| sesiones | minute | fecha    | 
+----------+--------+---------------------+ 
|  350 | 329 | 2014-12-26 12:49:02 | 
|  329 | 320 | 2014-12-26 12:48:01 | 
|  320 | 320 | 2014-12-26 12:47:02 | 
|  320 | 328 | 2014-12-26 12:46:01 | 
|  328 | 314 | 2014-12-26 12:45:02 | 
|  314 | 318 | 2014-12-26 12:44:01 | 
|  318 | 308 | 2014-12-26 12:43:02 | 
|  308 | 326 | 2014-12-26 12:42:01 | 
|  326 | 308 | 2014-12-26 12:41:02 | 
|  308 | NULL| 2014-12-26 12:40:01 | 

我嘗試以下查詢:

SELECT DATE_FORMAT(l.fecha,'%Y-%m-%d %H:%i'), 
     l.sesiones, 
     ( 
       SELECT sesiones 
       FROM bearsesiones 
       WHERE DATE_FORMAT(fecha,'%Y-%m-%d %H:%i') = Date_format(fecha - INTERVAL 1 MINUTE,'%Y-%m-%d %H:%i')) 'minute' 
FROM bearsesiones l; 

而結果:

| 2014-12-26 12:41     |  326 | NULL | 
| 2014-12-26 12:42     |  308 | NULL | 
| 2014-12-26 12:43     |  318 | NULL | 
| 2014-12-26 12:44     |  314 | NULL | 
| 2014-12-26 12:45     |  328 | NULL | 
| 2014-12-26 12:46     |  320 | NULL | 
| 2014-12-26 12:47     |  320 | NULL | 
| 2014-12-26 12:48     |  329 | NULL | 
| 2014-12-26 12:49     |  350 | NULL | 

編輯,添加字段AUTO_INCREMENT:

| 46 | 2014-12-26 12:41     |  326 | 
| 48 | 2014-12-26 12:42     |  308 | 
| 50 | 2014-12-26 12:43     |  318 | 
| 52 | 2014-12-26 12:44     |  314 | 
| 54 | 2014-12-26 12:45     |  328 | 
| 56 | 2014-12-26 12:46     |  320 | 
| 58 | 2014-12-26 12:47     |  320 | 
| 60 | 2014-12-26 12:48     |  329 | 
| 62 | 2014-12-26 12:49     |  350 | 

有何想法?

+0

這是一個很大的問題,而你有沒有主鍵。爲什麼不添加自動增量int ID字段? –

+0

沒問題,我可以放一個auto_increment。添加自動增量字段的想法是什麼? – InfoHD

回答

0
SELECT test1.sessiones, test2.sessiones AS 
MINUTE , test1.fecha 
FROM `test` AS test1 
LEFT JOIN test AS test2 ON (test1.id != test2.id 
AND (
    test2.fecha 
    BETWEEN DATE_FORMAT(DATE_SUB(test1.fecha, INTERVAL 1 MINUTE), "%Y-%m-%d %H:%i") 
    AND DATE_FORMAT(test1.fecha, "%Y-%m-%d %H:%i")) 
) 
ORDER BY test1.fecha DESC 
+0

謝謝,好主意添加ID字段。 – InfoHD

0
DROP TABLE IF EXISTS my_table; 

CREATE TABLE my_table 
(sesiones INT NOT NULL 
,fecha DATETIME NOT NULL 
,PRIMARY KEY(sesiones,fecha) 
); 

INSERT INTO my_table VALUES 
(350,'2014-12-26 12:49:02'), 
(329,'2014-12-26 12:48:01'), 
(320,'2014-12-26 12:47:02'), 
(320,'2014-12-26 12:46:01'), 
(328,'2014-12-26 12:45:02'), 
(314,'2014-12-26 12:44:01'), 
(318,'2014-12-26 12:43:02'), 
(308,'2014-12-26 12:42:01'), 
(326,'2014-12-26 12:41:02'), 
(308,'2014-12-26 12:40:01'); 

SELECT a.sesiones 
    , b.sesiones minute 
    , a.fecha 
    FROM 
    (
     SELECT x.*,COUNT(*) rank FROM my_table x JOIN my_table y ON y.fecha <= x.fecha GROUP BY x.fecha 
    ) a 
    LEFT 
    JOIN 
    (
     SELECT x.*,COUNT(*) rank FROM my_table x JOIN my_table y ON y.fecha <= x.fecha GROUP BY x.fecha 
    ) b 
    ON b.rank = a.rank-1 
ORDER 
    BY a.fecha DESC; 

+----------+--------+---------------------+ 
| sesiones | minute | fecha    | 
+----------+--------+---------------------+ 
|  350 | 329 | 2014-12-26 12:49:02 | 
|  329 | 320 | 2014-12-26 12:48:01 | 
|  320 | 320 | 2014-12-26 12:47:02 | 
|  320 | 328 | 2014-12-26 12:46:01 | 
|  328 | 314 | 2014-12-26 12:45:02 | 
|  314 | 318 | 2014-12-26 12:44:01 | 
|  318 | 308 | 2014-12-26 12:43:02 | 
|  308 | 326 | 2014-12-26 12:42:01 | 
|  326 | 308 | 2014-12-26 12:41:02 | 
|  308 | NULL | 2014-12-26 12:40:01 | 
+----------+--------+---------------------+