我有一张包含以下数据的表格:
+----------+---------------------+
| 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 :(得分: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
答案 1 :(得分: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 |
+----------+--------+---------------------+