MySQL用户变量的声明方式:
select @var_name := 0;
这样就可以声明一个变量并赋值为0.
使用方式:
如下面这个例子:
select a.person_name from
(
select turn,person_name,
@a:=@a+weight as sum_weight
from Queue,(select @a:=0) as t order by turn
) as a where a.sum_weight <= 1000 order by a.turn desc limit 1
重点是 select 某某 from Queue, (select @a:=0) as t
这一句, 它使用内链接, 在原来的表 Queue
中声明了一个用户变量 @a
. 然后, 就可以用这个变量对 weight
这个字段进行累加了(也就是 sum_weight
).
鄙人才疏学浅, 对MySQL用户变量的理解至为止. 感谢阅读~