7

I am wondering can you create an array variable in MySQL? I know that you can create a normal variable like so SET @var1 = "myvar"; but is there a way of creating an array? If so how?

Marc Delisle
  • 8,760
  • 3
  • 27
  • 29
user3144542
  • 589
  • 3
  • 8
  • 19

1 Answers1

15

You can create an array like so

SET @arrayVar = 'var1,var2,bar3,foo4';

It can be used thus

select from myTable where find_in_set(myTable.myColumn, @arrayVar);

If you want to create an array from a query, you can use temporary tables

create temporary table if not exists tmp_table select myColumn from myTable where 
Michael Zajac
  • 54,331
  • 7
  • 110
  • 133
Igbanam
  • 5,706
  • 4
  • 41
  • 66
  • works also on calculated value like : `find_in_set(month(mytable.myDate), @arrayVar)` – yoni Jul 21 '20 at 14:42