0

How to pass a MySQL variable to a sub query something like as illustrated below.

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

1 Answer 1

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

Demo

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

Sign up to request clarification or add additional context in comments.

1 Comment

What if there are two tables say table1 and table2? Means, something like select v1.val1, v2.val2, @var:= @var + 1 var from (select val1 from table1 where id=@var) v1, (select val2 from table2 where id=@var) v2, (select @var:= 0) var

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.