mysql变量存在问题并且计数
代码如下:
SELECT @var := 2, @var := @var+2, count(birey_dil.birey_id) as count, @var := @var+count(birey_dil.birey_id), @var+1 from birey_dil
以下是结果集:
+-----------+----------------+-------+----------------------------------------+--------+ | @var := 2 | @var := @var+2 | count | @var := @var+count(birey_dil.birey_id) | @var+1 | +-----------+----------------+-------+----------------------------------------+--------+ | 2 | 4 | 8 | 12 | 5 | +-----------+----------------+-------+----------------------------------------+--------+
我将var设置为2
然后2 + 2 = 4
然后我计算行数,结果是8
@var:= @ var + count(birey_dil.birey_id)
返回4 + 8 12 但 @ var + 1为5
为什么mysql会这样呢?
@ var + 1现在应该是13并且有办法解决这个问题吗?
答案 0 :(得分:1)
试试这个:
SELECT @var+1 FROM
似乎mysql首先评估所有@var [+ constant];这是有道理的,因为它不需要数据库访问,然后才计算
(
SELECT @var := 2, @var := @var+2,
count(birey_dil.birey_id) as count,
@var := @var+count(birey_dil.birey_id),
from birey_dil
)x
count(birey_dil.birey_id)
。最后它是@var := @var+count
。