You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PostgREST实现PostgreSQL多列组合IN条件查询?

PostgREST实现多字段组合IN查询的方法

问题场景

现有PostgreSQL表test_1,结构及数据如下:

IDcontainer_idproduct_id
111
212
321
422

需要执行的目标SQL查询为:

select * from test_1 where (container_id,product_id) in ((1,1),(1,2),(2,1));

该查询应返回ID为1至3的行。

当前构造的PostgREST请求URL为:

http://localhost:8080/test_1?&product_id=in.(1,2)&container_id=in.(1,2)

但此URL会返回所有4行,不符合需求——因为它对应的SQL是where container_id in (1,2) AND product_id in (1,2),是两个字段各自独立的IN条件,而非字段组合的匹配逻辑。

解决方案

PostgREST支持多字段组合的IN查询,需要使用行构造器配合in操作符,正确的请求URL格式如下:

http://localhost:8080/test_1?and=(container_id,product_id).in.((1,1),(1,2),(2,1))

这个URL对应的SQL逻辑完全匹配目标查询,会精准返回ID为1、2、3的行,满足需求。

内容的提问来源于stack exchange,提问作者renaise21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:50:18