LAST_VALUE()

语法

LAST_VALUE(expr,[expr,...])

描述

LAST_VALUE() 对所有表达式求值,并返回最后一个值。

这在结合使用@var:=expr为变量赋值时很有用。例如,当你想要从updated/deleted的行中获取数据时,你无需对表做两次查询。

MariaDB 10.2.2开始,LAST_FUNCTION可以用作为一个开窗函数window function

示例

CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES(1,10),(2,20);
DELETE FROM t1 WHERE a=1 AND last_value(@a:=a,@b:=b,1);
SELECT @a,@b;
+------+------+
| @a   | @b   |
+------+------+
|    1 |   10 |
+------+------+

//译者补充示例
DELETE FROM t1;
INSERT INTO t1 VALUES(1,10),(2,20),(1,30);
DELETE FROM t1 WHERE a=1 AND last_value(@a:=a,@b:=b,1);
SELECT *,@a,@b;
+------+------+------+------+
| a    | b    | @a   | @b   |
+------+------+------+------+
|    2 |   20 |    1 |   30 |
+------+------+------+------+

See also

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.