包含列的employees表:
id (pk) BIGINT,名称文本,电子邮件文本,work_locations BIGINT[]。
work_locations列包含位置表ids。
有列的位置表:
id (pk) BIGINT,lat小数点(12,9),长小数(12,9)。
我想要一个类似的结果
id name email lat, long
1 name email@email.com 23.345 54.3678我无法连接work_locations和ids上的两个表。
如何连接这两张桌子?
发布于 2021-03-25 12:12:56
您可以使用ANY运算符加入:
select e.*,
l.lat,
l.long
from employees e
join locations l on l.id = any(e.work_locations)
order by e.id;通常,我建议而不是将外键存储在类似的数组中。您应该考虑使用employees和locations之间具有多到多个链接表的适当规范化模型。
发布于 2021-03-25 12:09:52
你可以unnest()
select e.*, l.*
from employees e cross join lateral
unnest(work_locations) wl(work_location) left join
locations l
on wl.work_location = l.id;https://stackoverflow.com/questions/66799131
复制相似问题