在 PostgreSQL WHERE 子句中,如何查找 ID 不在数组中的所有行?

发布于 2024-11-08 19:28:44 字数 546 浏览 0 评论 0原文

好的,我有一个存储过程...我如何找到 ID 不在数组中的所有行? (请记住,我使用的是在创建存储过程时动态创建的 PostgreSQL 数组,

示例:

|  people   |
-------------
| id | Name |
-------------
|  1 | Bob  |
|  2 | Te   |
|  3 | Jack |
|  4 | John |

该数组有 somePGArray := [1,3],因此伪查询将如下所示:

SELECT * FROM people WHERE id NOT IN (somePGArray)

结果查询

|  people   |
-------------
| id | Name |
-------------
|  2 | Te   |
|  4 | John |

:额外的好处是,我也不知道如何创建一个数组并向其添加 ID,所以如果您有一个快速提示如何做到这一点,那将会非常有帮助:-)。

Okay, I've got a stored procedure... how do I find all rows whose ID's are not in an array? (Keep in mind that I'm using a PostgreSQL array that is created dynamically when the stored procedure is created

Example:

|  people   |
-------------
| id | Name |
-------------
|  1 | Bob  |
|  2 | Te   |
|  3 | Jack |
|  4 | John |

The array has somePGArray := [1,3], so a psuedo-query would look like this:

SELECT * FROM people WHERE id NOT IN (somePGArray)

Resulting query:

|  people   |
-------------
| id | Name |
-------------
|  2 | Te   |
|  4 | John |

As a bonus, I also have no idea how to create an array and append ID's to it, so if you have a quick hint how to do that, that'd be tremendously helpful. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韵柒 2024-11-15 19:28:44
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);
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);
七颜 2024-11-15 19:28:44

如果您正在谈论实际的 PostgreSQL 数组,请使用

SELECT * FROM people WHERE NOT id = ANY ('{1,3}')

If you're talking about an actual PostgreSQL array, use

SELECT * FROM people WHERE NOT id = ANY ('{1,3}')
蓝天 2024-11-15 19:28:44

只需删除方括号:

WHERE id NOT IN (1,2)

作为奖励,您的 NOT IN 子句可以填充一个子查询,如下所示:

Where id not in (select id from sometable where some_field = somevalue)

您还可以执行动态字符串连接来生成逗号分隔集并将其注入临时查询中。

Just remove the square braces:

WHERE id NOT IN (1,2)

As a bonus, your NOT IN clause could be populated with a subquery that would look like:

Where id not in (select id from sometable where some_field = somevalue)

You could also do a dynamic string concatenation to generate a comma-separated set and inject it into an ad hoc query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文