带有变量的MySQL子查询

时间:2018-04-11 09:41:24

标签: mysql variables subquery

如何将MySQL变量传递给子查询,如下所示。

select
 v.val, @var:= @var + 1 var 
from 
 (select val from table where id=@var) v, 
 (select @var:= 0) var

1 个答案:

答案 0 :(得分:1)

select
 v.val ,var
from 
(select val, @var:= @var + 1 var from Table1,(select @var:= 0) var where id=@var+1) v

<强>演示

  

http://sqlfiddle.com/#!9/765e9c/14