让我们首先创建一个表-mysql> create table DemoTable(Value int);
使用插入命令在表中插入一些记录-mysql> insert into DemoTable values(50);
mysql> insert into DemoTable values(20);
mysql> insert into DemoTable values(30);
使用select语句显示表中的所有记录-mysql> select *from DemoTable;
这将产生以下输出-+-------+
| Value |
+-------+
| 50 |
| 20 |
| 30 |
+-------+
3 rows in set (0.00 sec)
这是执行MySQL中前几行之和的前几行的查询-mysql> select t.Value,
(@s := @s + t.Value) as Number
from DemoTable t cross join
(select @s := 0) p
order by t.Value;
这将产生以下输出-+-------+--------+
| Value | Number |
+-------+--------+
| 20 | 20 |
| 30 | 50 |
| 50 | 100 |
+-------+--------+
3 rows in set (0.07 sec)