函数内的mysql赋值

时间:2013-02-13 00:15:18

标签: mysql variables variable-assignment datediff

我有一个带有DATETIME列的历史记录表。是否无法在DATEDIFF函数中分配变量?以下声明:

SET @LASTDATETIME='2000-01-01 00:00:00';
SELECT DATETIME, @LASTDATETIME, TIMEDIFF(DATETIME, @LASTDATETIME:=DATETIME) AS CALC, @LASTDATETIME FROM HISTORY

导致这个结果:

|DATETIME           |@LASTDATETIME       | CALC    | @LASTDATETIME
-------------------------------------------------------------------
2013-01-28 12:11:41 |2000-01-01 00:00:00 |00:00:00 |[BLOB - 19 B]
2013-01-28 12:11:44 |2013-01-28 12:11:41 |00:00:00 |[BLOB - 19 B]

我不明白为什么在DATETIME被转移到下一条记录时,分配的工作类似,但CALC字段失败了,在分配后,@ LASTDATETIME的值是BLOB?为什么这不能像我期待的那样工作?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT @last AS `last`,
       TIMEDIFF(`datetime`, @last) AS `difference`,
       @last := `datetime` AS `current`
FROM history, (SELECT @last :='2000-01-01 00:00:00') n

这是sqlfiddle