The following works as expected when there is a single value stored in a variable:
Set @var = 121;
select * from table where id = @var;
How can I set variable with multiple value and then use it in a query. I have tried this but it doesn't work:
set @var = (
117,120,121,122,143,151,175,233,387,189,118,119,339,357,500,501,493,425,307,191,
117,120,121,122,143,151,175,233,387,189,118,119,339,357,500,501,493,425,307,191,
117,120,121,122,143,151,175,233,387,189,118,119,339,357,500,501,493,425,307,191
)
select * from table where id = @var;
SET @a := "20100630"; SELECT * FROM wordbase WHERE verified = @a; But it does not work when there are multiple values stored in a variable. SET @a := "'20100630', '20100701' "; SELECT * FROM wordbase WHERE verified in (@a);
Assigning multiple values to multiple variables If you have to populate multiple variables, instead of using separate SET statements each time consider using SELECT for populating all variables in a single statement. This can be used for populating variables directly or by selecting values from database.
set @var = '
117,120,121,122,143,151,175,233,387,189,118,119,339,357,500,501,493,425,307,191,
117,120,121,122,143,151,175,233,387,189,118,119,339,357,500,501,493,425,307,191,
117,120,121,122,143,151,175,233,387,189,118,119,339,357,500,501,493,425,307,191'
SELECT * FROM table WHERE FIND_IN_SET(id,@var);
Thanks buddy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With