Answer the question
In order to leave comments, you need to log in
PgSQL. How to apply a condition in the selection, only if the field is not empty?
Good afternoon! Please tell me how to make a selection (without using functions) so that the condition works only if the field is not an empty array. Let me explain with an example:
There are 2 tables
CREATE TABLE public.t1
(
id character varying(24) NOT NULL,
ratings integer[]
)
CREATE TABLE public.t2
(
id character varying(24) NOT NULL,
rating integer
)
SELECT * FROM t1
INNER JOIN t2 ON IF t1.ratings.length > 0 THEN t2.rating = any(t1.ratings) ELSE true
Answer the question
In order to leave comments, you need to log in
Understood. The solution, as always, lay on the surface, may be useful to someone:
SELECT * FROM t1
INNER JOIN t2 ON array_length(t1.ratings,1) IS NULL OR t2.rating = any(t1.ratings)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question