LINUX.ORG.RU

Проверка наличия элемента в Postgresql

 ,


0

2
# select 1 = any ('{1}'::bigint[]);
 ?column? 
----------
 t
(1 row)

# select parent_cats from cats where id = 2;
 parent_cats 
-------------
 {1}
(1 row)

# select * from cats where id = any (select parent_cats from cats where id = 2);
ERROR:  operator does not exist: bigint = bigint[]
LINE 1: select * from cats where id = any (select parent_cats from c...
                                    ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
#

Почему не работает последнее? Как правильно написать?


Ответ на: комментарий от anonymous
ERROR:  operator does not exist: bigint = bigint[]
LINE 1: select * from cats where id in (select parent_cats from cats...
                                    ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
iluha16
() автор топика

Причина в том что возвращается array результатов (строчек результата может же быть больше одной). Вот так работает:

select * from cats where id = any (select unnest(parent_cats) from cats where id = 2);
iluha16
() автор топика

select * from cats where id = any ((select parent_cats from cats where id = 2)::bigint[]);

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.