Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql Select rows where column = array

Tags:

postgresql

This is a summary of what I am trying to do:

$array[0] = 1;
$array[1] = 2;

$sql = "SELECT * FROM table WHERE some_id = $array"

Obviously, there are some syntax issues, but this is what I want to do, and I haven't found anything yet that shows how to do it.

Currently, my plan is to do something along these lines:

foreach($idList as $is)
    $where .= 'some_id=' . $id . ' OR';
endforeach

$sql = "SELECT * FROM table WHERE " . $where;

So is there support in PostgreSQL to use an array to search, or do I have to do something similar to my solution?

like image 252
Jimmy Pitts Avatar asked May 24 '12 13:05

Jimmy Pitts


People also ask

Can we use array in where clause?

So, our using the WHERE IN clause we can fetch the rows and columns from the database table array by following and validating a condition to result required set of output table in the MySQL server.

How do I query an array of columns in SQL?

Step 1: Group the data by the field you want to check. Step 2: Left join the list of required values with the records obtained in the previous step. Step 3: Now we have a list with required values and corresponding values from the table.

How do you query an array?

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { <array field>: { <operator1>: <value1>, ... } }


4 Answers

SELECT  *
FROM    table
WHERE   some_id = ANY(ARRAY[1, 2])

or ANSI-compatible:

SELECT  *
FROM    table
WHERE   some_id IN (1, 2)

The ANY syntax is preferred because the array as a whole can be passed in a bound variable:

SELECT  *
FROM    table
WHERE   some_id = ANY(?::INT[])

You would need to pass a string representation of the array: {1,2}

like image 145
Quassnoi Avatar answered Oct 12 '22 19:10

Quassnoi


For dynamic SQL use:

'IN(' ||array_to_string(some_array, ',')||')'

Example

DO LANGUAGE PLPGSQL $$

DECLARE
    some_array bigint[];
    sql_statement text;

BEGIN

    SELECT array[1, 2] INTO some_array;
    RAISE NOTICE '%', some_array;

    sql_statement := 'SELECT * FROM my_table WHERE my_column IN(' ||array_to_string(some_array, ',')||')';
    RAISE NOTICE '%', sql_statement;

END;

$$;

Result: NOTICE: {1,2} NOTICE: SELECT * FROM my_table WHERE my_column IN(1,2)

like image 34
Ufos Avatar answered Oct 12 '22 20:10

Ufos


In my case, I needed to work with a column that has the data, so using IN() didn't work. Thanks to @Quassnoi for his examples. Here is my solution:

SELECT column(s) FROM table WHERE expr|column = ANY(STRING_TO_ARRAY(column,',')::INT[])

I spent almost 6 hours before I stumble on the post.

like image 5
jking Avatar answered Oct 12 '22 21:10

jking


   $array[0] = 1;
   $array[2] = 2;
   $arrayTxt = implode( ',', $array);
   $sql = "SELECT * FROM table WHERE some_id in ($arrayTxt)"
like image 3
Alejandro Salamanca Mazuelo Avatar answered Oct 12 '22 21:10

Alejandro Salamanca Mazuelo