How to select rows for which column has empty array ?

Started by Arup Rakshitalmost 12 years ago3 messagesgeneral
Jump to latest
#1Arup Rakshit
aruprakshit@rocketmail.com

I have a below table :

yelloday_development=# select id,workplace_ids,team_ids  from reporting_groups ;                                                                                                                                                                                     
 id | workplace_ids | team_ids 
----+---------------+----------
  2 | {}            | {}
  3 | {}            | {}
  1 | {}            | {}
  4 | {1}           | {1}
  5 | {2}           | {2,3,4}
(5 rows)

yelloday_development=# select id from reporting_groups where 2 = ANY (team_ids)
yelloday_development-# ;
 id 
----
  5
(1 row)

How would I select rows which has empty array for the field "team_ids" ?
 
Regards,
Arup Rakshit

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Arup Rakshit (#1)
Re: How to select rows for which column has empty array ?

Arup Rakshit wrote

I have a below table :

yelloday_development=# select id,workplace_ids,team_ids  from
reporting_groups ;                                                        
                                                                         
                                                  
 id | workplace_ids | team_ids 
----+---------------+----------
  2 | {}            | {}
  3 | {}            | {}
  1 | {}            | {}
  4 | {1}           | {1}
  5 | {2}           | {2,3,4}
(5 rows)

yelloday_development=# select id from reporting_groups where 2 = ANY
(team_ids)
yelloday_development-# ;
 id 
----
  5
(1 row)

How would I select rows which has empty array for the field "team_ids" ?

The generic way would be to checks its length:

WHERE array_length(team_ids, 1) = 0

you could also check for equality with the empty array:

WHERE teams_ids = array[]::integer[]

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/How-to-select-rows-for-which-column-has-empty-array-tp5806338p5806343.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Bosco Rama
postgres@boscorama.com
In reply to: Arup Rakshit (#1)
Re: How to select rows for which column has empty array ?

On 06/06/14 07:30, Arup Rakshit wrote:

How would I select rows which has empty array for the field "team_ids" ?

The shortest way is to test against an empty array constant:
select id from reporting_groups where team_ids = '{}';

Or you can test using array_length(). Or you test against an empty
array[], cast appropriately.

HTH,
Bosco.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general