【发布时间】:2022-02-12 08:03:49
【问题描述】:
这是我的查询的样子:
select "spaces"."id" as "id",
"spaces"."user_id" as "user_id",
"spaces"."type" as "type",
"spaces"."latitude" as "latitude",
"spaces"."longitude" as "longitude",
"categories"."category_id" as "categories:category_id"
from "spaces"
left join "spaces_categories" as "categories" on "categories"."space_id" = "spaces"."id"
where "categories"."category_id" in ($)
and ST_DWithin(spaces.geometry::geography, ST_SetSRID(ST_Point($,$), 4326)::geography, $)
我正在使用第 14.1 页
我正在尝试查找半径内满足请求类别 ID 的所有空间,以及这些空间的所有类别。
一个空间可以有很多类别 ID,所以如果我搜索类别 ID (1,2),并且恰好有 2 个空间结果(假设每个空间有 4 个类别,1,2,3,4),我会期待 8 行.但是,对于类别 1,2,我的查询仅返回 4 行。
如何更新我的查询,以便获得所有类别,只要有一些重叠?
是因为我的IN 子句吗?
【问题讨论】:
-
查询格式错误。谓词
where "categories"."category_id" in ($)默默地将外连接转换为内连接。请通过以下方式修改查询:1) 使用内部联接,或 2) 通过接受"categories"."category_id"中的空值。 -
谢谢,抱歉,您能解释一下修改查询的意思吗?您是说将 where 向下移动(在 postgis 函数之前),然后使用,所以它在连接上?
-
I am trying to find spaces within a radius, and that meet some categories input.与get all the categories, as long there is some overlap不同。请确切地说明您想要什么。首先公开你的 Postgres 版本。 -
啊,我明白了,谢谢!我已经更新了
标签: sql postgresql join postgis