Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Variable With Multiple Values

Tags:

mysql

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;
like image 285
mdotk Avatar asked Oct 15 '15 09:10

mdotk


People also ask

How can I SET multiple values in one variable in MySQL?

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);

How do I assign multiple values to a single variable in SQL?

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.


1 Answers

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

like image 66
jai dutt Avatar answered Sep 20 '22 15:09

jai dutt