MySQL SELECT 将列值与上一个值相加
mysqlmysqli database更新于 2025/11/2 14:36:17
为此,您可以使用会话变量。让我们首先创建一个表 −
mysql> create table DemoTable809(Price int); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable809 values(40); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable809 values(50); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable809 values(60); Query OK, 1 row affected (0.12 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable809;
这将产生以下输出 −
+-------+ | Price | +-------+ | 40 | | 50 | | 60 | +-------+ 3 rows in set (0.00 sec)
以下查询将某一列的值与先前的值相加 −
mysql> select @currentSum:=@currentSum+Price from DemoTable809;
这将产生以下输出 −
+--------------------------------+ | @currentSum:=@currentSum+Price | +--------------------------------+ | 40 | | 90 | | 150 | +--------------------------------+ 3 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

