在 PostgreSQL WHERE 子句中,如何查找 ID 不在数组中的所有行?
好的,我有一个存储过程...我如何找到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在谈论实际的 PostgreSQL 数组,请使用
If you're talking about an actual PostgreSQL array, use
只需删除方括号:
作为奖励,您的 NOT IN 子句可以填充一个子查询,如下所示:
您还可以执行动态字符串连接来生成逗号分隔集并将其注入临时查询中。
Just remove the square braces:
As a bonus, your NOT IN clause could be populated with a subquery that would look like:
You could also do a dynamic string concatenation to generate a comma-separated set and inject it into an ad hoc query.