Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres array lookup in WHERE clause

Tags:

People also ask

Can we use array in where clause?

We can pass an array with the help of where IN clause. Let us first create a new table for our example.

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>, ... } }

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.


I have a query:

SELECT bar, (SELECT name FROM names WHERE value = bar) as name
FROM foobar WHERE foo = 1 and bar = ANY (1,2,3)

My problem is, when there is no row containing bar = 3 (or whatever other value is requested) in table foobar, no rows are returned for that value of bar.

I'd like my query to return a row of [bar, NULL] instead, but can't think up a way to approach this.

Is this even possible?