Postgres select rows where ANY in a given array = ANY in column array - postgresql

In Postgres, I need to select all rows where any value in an array (passed as variable) is equal to any value in the column (that is also an array). This means something like this:
SELECT *
from table
where ANY (value_in_an_array_variable) = ANY (value_in_a_column_array);
If there is no direct way what's the best alternative?

You are looking for the overlaps ("have elements in common") operator:
select *
from some_table
where array_column && array[1,2,3];

Related

Query to search over array elements inside jsonb PSQL

I have a JSON node on which I have to write a PSQL query, My table schema name(String),tagValues(jsonb). Example tagValue data is given below
Name_TagsTable
uid | name(String)| tagValues(jsonb)
-----+-------------------+-----------------------------
1 | myName | { "tags": ["xyz","pqr","xyp"]}
I need a query that returns all rows for a search "pq" made on the tagValues of the table
select * from Name_TagsTable where tagValues->tags contains %pq%
You can use LIKE operator along with casting JSONB value to a string type such as
SELECT *
FROM Name_TagsTable
WHERE (tagValues->'tags')::TEXT LIKE '%pq%'
You need to unnest the elements, then you can use it in a WHERE condition that applies a LIKE condition.
select nt.*
from name_tagstable nt
where exists (select *
from jsonb_array_elements_text(tagvalue -> 'tags') as a(tag)
where a.tag like '%pg%');

How to re-map array column values in select in Postgresql?

Is it possible to re-map integer values from a Postgres array column in the select? This is what I have:
select unnest(tag_ids) from mention m where id = 288201;
unnest
---------
-143503
-143564
125192
143604
137694
tag_ids is integer[] column
I would like to translate those numbers. Functions like abs(unnest(..)) work but found I cannot use a CASE statement. Tx.
If you want to do anything non-trivial with the elements from an array after unnesting, use the set-returning function like table:
select u.tag_id
from mention m
cross join unnest(m.tag_ids) as u(tag_id)
where m.id = 288201;
Now, u.tag_id is an integer column that you can use like any other column, e.g. in a CASE expression.

Select rows in postgres table where an array field contains NULL

Our system uses postgres for its database.
We have queries that can select rows from a database table where an array field in the table contains a specific value, e.g.:
Find which employee manages the employee with ID 123.
staff_managed_ids is a postgres array field containing an array of the employees that THIS employee manages.
This query works as expected:
select *
from employees
where 123=any(staff_managed_ids)
We now need to query where an array field contains a postgres NULL. We tried the following query, but it doesn't work:
select *
from employees
where NULL=any(staff_managed_ids)
We know the staff_managed_ids array field contains NULLs from other queries.
Are we using NULL wrongly?
NULL can not be compared using =. The only operators that work with that are IS NULL and IS NOT NULL.
To check for nulls, you need to unnest the elements:
select e.*
from employees e
where exists (select *
from unnest(e.staff_managed_ids) as x(staff_id)
where x.staff_id is null);
if all your id values are positive, you could write something like this:
select *
from employees
where (-1 < all(staff_managed_ids)) is null;
how this works is that -1 should be less than all values, however comparison with null will make the whole array comparison expression null.

Postgresql ALL with IN

Without using a subquery I'd like to find if all the elements in an array are equal to a subset of numbers. So instead of 1 = ALL(ARRAY[1,1,1]) I want to do something like ALL(ARRAY[1,1,1]) IN (1, 5). Is this possible without using a select statement?
You want to use the #> operator.
-- does the column contain all of
select * from test_arrays where values #> array[6, 9];
select * from test_arrays where values #> '{6, 9}'::int[];
If you want to find where any 1 value of the array is in the other array use the && operator:
-- does the column contain at-least one of
select * from test_arrays where values && array[6, 9];
select * from test_arrays where values && '{6, 9}'::int[];
I happened to write about this a couple of months ago.
http://www.philliphaydon.com/2016/05/07/postgresql-and-its-array-datatype/

PostgreSQL: check if attribute is in table

I'm writing some triggers to check if an arrtibute of a newly create tuple is present in another table. What is the best way to check this?
IF (SELECT * FROM TABLEB where NEW.Attribute = Attribute) = NULL THEN
return NULL
END IF
Is there a better way? And does a Select that returns nothing = NULL or empty set?
Use IF EXISTS (SELECT * FROM TABLEB where NEW.Attribute = Attribute).
An empty rowset isn't the same as NULL, which represents an empty cell of a row. Your syntax would only be valid if the query only selected one row, in which case PostgreSQL will allow use of comparison operators against the top cell returned, which counts as NULL if none are returned.