Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating array variable in MySQL

Tags:

mysql

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?

like image 272
user3144542 Avatar asked Dec 30 '13 18:12

user3144542


People also ask

What is array in MySQL with example?

An array is type of data structure defined in MySQL. MySQL provides WHERE IN clause that is useful to apply in the array variable to produce the query set from specific table in the database. WHERE IN clause supports to fetch data values from an array of parameters provided in the query statement in MySQL.

How to create an array of values in SQL?

Also, there is no array syntax in SQL - you'd have to use: SELECT 2 FROM DUAL UNION ALL SELECT 34 FROM DUAL UNION ALL SELECT 24 FROM DUAL ... to construct your "array of values" equivalent in SQL. SQL scripts would have individual INSERT statements.

How to assign a value to a variable in MySQL?

The second way to assign a value to a variable is to use the SELECT statement. In this case, you must use the := assignment operator because, within the SELECT statement, MySQL treats the = operator as the equal operator. SELECT@variable_name := value; Code language:SQL (Structured Query Language)(sql)

How to use MySQL where in array with TableName?

TableName is the specified table holding array values in the database to use MySQL WHERE IN Array. After WHERE clause, specific column name is defined from which the data is retrieved using SELECT statement and from which the IN () function includes the range of values as parameters.


1 Answers

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 
like image 187
Igbanam Avatar answered Nov 10 '22 18:11

Igbanam