WHERE 子句中的 Postgres 数组查找
我有一个查询:
SELECT bar, (SELECT name FROM names WHERE value = bar) as name
FROM foobar WHERE foo = 1 and bar = ANY (1,2,3)
我的问题是,当表 foobar
中没有包含 bar = 3
的行(或请求的任何其他值)时,不会返回任何行酒吧的价值。
我希望我的查询返回一行 [bar, NULL]
,但无法想出解决此问题的方法。
这可能吗?
I have a query:
SELECT bar, (SELECT name FROM names WHERE value = bar) as name
FROM foobar WHERE foo = 1 and bar = ANY (1,2,3)
My problem is, when there is no row containing bar = 3
(or whatever other value is requested) in table foobar
, no rows are returned for that value of bar.
I'd like my query to return a row of [bar, NULL]
instead, but can't think up a way to approach this.
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许类似这种方法就是您所追求的:
testbed:
原始方法:
替代方法:
如果您使用的是8.3或之前的版本,则没有内置的
unnest
功能,但您可以推出自己的(效率不高)替换:Perhaps something like this approach is what you are after:
testbed:
original method:
alternative method:
If you are on 8.3 or before, there is no built-in
unnest
function, but you can roll your own (not very efficient) replacement:请尝试该查询。
Try that query instead.