无法弄清楚为什么 @previousEndTime
变量只有 YEAR
值。
我的 test_table 上的示例数据:
| id | name | start | end |
|----|--------|---------------------------|---------------------------|
| 2 | test 1 | January, 01 2016 12:00:00 | January, 01 2016 13:00:00 |
| 3 | test 1 | January, 02 2016 11:00:00 | January, 02 2016 12:00:00 |
| 5 | test 1 | January, 03 2016 15:00:00 | January, 03 2016 16:00:00 |
| 6 | test 2 | January, 01 2016 10:00:00 | January, 01 2016 11:00:00 |
| 7 | test 2 | January, 02 2016 17:00:00 | January, 02 2016 18:00:00 |
SELECT
id,
name,
@previousEndTime,
@previousEndTime := end
FROM
test_table, (SELECT @previousEndTime := 0) var
ORDER BY name, id;
当前结果:
| id | name | @previousEndTime | @previousEndTime := end |
|----|--------|------------------|-------------------------|
| 2 | test 1 | 0 | 2016-01-01 13:00:00 |
| 3 | test 1 | 2016 | 2016-01-02 12:00:00 |
| 5 | test 1 | 2016 | 2016-01-03 16:00:00 |
| 6 | test 2 | 2016 | 2016-01-01 11:00:00 |
| 7 | test 2 | 2016 | 2016-01-02 18:00:00 |
预期结果:
| id | name | @previousEndTime| @previousEndTime := end |
|----|--------|---------------------|-------------------------|
| 2 | test 1 | 0 | 2016-01-01 13:00:00 |
| 3 | test 1 |2016-01-01 13:00:00 | 2016-01-02 12:00:00 |
| 5 | test 1 |2016-01-02 12:00:00 | 2016-01-03 16:00:00 |
| 6 | test 2 |2016-01-03 16:00:00 | 2016-01-01 11:00:00 |
| 7 | test 2 |2016-01-01 11:00:00 | 2016-01-02 18:00:00 |
请检查这个FIDDLE
注意:
我尝试过的其他方法:
@previousEndTime := CONCAT('',end,'')
@previousEndTime := TIMESTAMP(CONCAT('',end,''))
最佳答案
看看这个;)
MySQL 5.6 模式:
DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 NOT NULL,
`start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`end` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
INSERT INTO `test_table` VALUES ('2', 'test 1', '2016-01-01 12:00:00', '2016-01-01 13:00:00');
INSERT INTO `test_table` VALUES ('3', 'test 1', '2016-01-02 11:00:00', '2016-01-02 12:00:00');
INSERT INTO `test_table` VALUES ('5', 'test 1', '2016-01-03 15:00:00', '2016-01-03 16:00:00');
INSERT INTO `test_table` VALUES ('6', 'test 2', '2016-01-01 10:00:00', '2016-01-01 11:00:00');
INSERT INTO `test_table` VALUES ('7', 'test 2', '2016-01-02 17:00:00', '2016-01-02 18:00:00');
查询 1:
SELECT
id,
name,
if(@previousEndTime='0000-00-00 00:00:00',0,@previousEndTime),
@previousEndTime := end
FROM
test_table,
(
SELECT
@previousEndTime := '0000-00-00 00:00:00'
) var
ORDER BY name, id
Results :
| id | name | if(@previousEndTime='0000-00-00 00:00:00',0,@previousEndTime) | @previousEndTime := end |
|----|--------|---------------------------------------------------------------|-------------------------|
| 2 | test 1 | 0 | 2016-01-01 13:00:00 |
| 3 | test 1 | 2016-01-01 13:00:00 | 2016-01-02 12:00:00 |
| 5 | test 1 | 2016-01-02 12:00:00 | 2016-01-03 16:00:00 |
| 6 | test 2 | 2016-01-03 16:00:00 | 2016-01-01 11:00:00 |
| 7 | test 2 | 2016-01-01 11:00:00 | 2016-01-02 18:00:00 |
关于mysql - 如何为 MySQL 变量设置时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289755/