2016-05-18 40 views
4

找不到@previousEndTime變量僅具有YEAR值的原因。如何設置一個MySQL變量的時間戳?

我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,''))  

回答

2

看看這個;)

SQL Fiddle

的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 | 
+0

謝謝里諾。 \t 是這樣的:在初始化部分變量的數據類型是確定的? –

+0

是的,我說得對。看一下[User-Defined Variables](http://dev.mysql.com/doc/refman/5.7/en/user-variables.html)。 – Blank

1

試試這個,

SELECT 
     id, 
     name, 
     @previousEndTime, 
     @previousEndTime := end  
    FROM 
     test_table, 
     (
      SELECT 
       @previousEndTime := '0000-00-00 00:00:00' 
     ) var 
    ORDER BY name, id; 

MySQL以YYYY格式顯示YEAR值,範圍爲1901至2155或0000.

作爲範圍爲'0'至'99'的1位或2位數字符串。 MySQL將範圍'0'到'69'和'70'到'99'的值轉換爲2000到2069和1970到1999年的範圍內的YEAR值。

插入數字0的結果具有顯示值爲0000,內部值爲0000.要插入零並將其解釋爲2000,請將其指定爲字符串'0'或'00'。

http://dev.mysql.com/doc/refman/5.7/en/year.html

+0

是這樣的:在初始化部分變量的數據類型是確定的? –

+0

是的,派生列的數據類型是從第一條記錄開始的! – Hytool

+0

非常感謝。自從裏諾首先回答我應該接受他的回答。我所能做的就是提高你的答案。謝謝哥們。 –