好的,我有一个存储过程。。。如何查找ID不在数组中的所有行?(请记住,我使用的是在创建存储过程时动态创建的PostgreSQL数组
例子:
| people |
-------------
| id | Name |
-------------
| 1 | Bob |
| 2 | Te |
| 3 | Jack |
| 4 | John |
数组有somePGArray:=[1,3],因此psuedo查询如下所示:
SELECT * FROM people WHERE id NOT IN (somePGArray)
结果查询:
| people |
-------------
| id | Name |
-------------
| 2 | Te |
| 4 | John |
另外,我也不知道如何创建一个数组并将I d附加到其中,所以如果您有一个快速的提示如何做到这一点,那将非常有帮助。:-)
最佳答案
create table foo1 ( id integer, name text );
insert into foo1 values (1,'Bob'),(2,'Te'),(3,'Jack'),(4,'John');
select * from foo1 where id not in (1,2);
select * from foo1 where not (id = ANY(ARRAY[1,2]));
create or replace function so_example(int)
returns SETOF foo1 as $$
declare
id alias for $1;
idlist int[] := '{1}';
q text;
rec record;
begin
idlist := idlist || ARRAY[id];
q := 'select * from foo1 where not (id = ANY('||quote_literal(idlist)||'))';
raise notice 'foo % %', idlist,q;
for rec in execute(q) loop
return next rec;
end loop;
end; $$
language 'plpgsql';
select * from so_example(3);