Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from array in MySQL

Tags:

mysql

Is it possible in MySQL to select from a static set of integers? Given an array of integers like 1 and 2, what is the correct syntax in MySQL to do something like:

select 
    * 
from 
    (values(1),(2)) 

Which should return one row containing the number 1 and one row containing the number 2.

In other SQL than MySQL (e.g. MSSQL) this is possible. Is there a way to do this in MySQL?

like image 289
Chris Avatar asked Mar 25 '12 12:03

Chris


People also ask

How do I query an array in MySQL?

mysql> select *from PassingAnArrayDemo; The following is the output. The following is the syntax to send an array parameter with the help of where IN clause. mysql> SELECT * -> FROM PassingAnArrayDemo where id IN(1,3,6);

What is SELECT * from in MySQL?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

Can you use array in MySQL?

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 do I SELECT within a selection in MySQL?

In MySQL subquery can be nested inside a SELECT, INSERT, UPDATE, DELETE, SET, or DO statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =.


2 Answers

I think you mean something like this?

SELECT 1 UNION SELECT 2 UNION SELECT 3
like image 178
MichaelRushton Avatar answered Sep 17 '22 17:09

MichaelRushton


sorry for my english

you can use (IN) like this

SELECT * FROM Table WHERE id IN (1,2,3....) 
like image 44
ALI ATTALAOUI Avatar answered Sep 20 '22 17:09

ALI ATTALAOUI