使用“in”在加入
我有两个选择 a & b 和我像这样加入他们:
select * from
(
select n.id_b || ',' || s.id_b ids, n.name, s.surname
from names n,
surnames s where n.id_a = s.id_a
) a
left join
(
select sn.id, sn.second_name
) b on b.id in (a.ids)
在这种情况下加入不起作用:( 问题出在(a.ids) 中的b.id 中。但为什么如果它看起来像 (12,24) 中的 12 并且没有结果:(
i have two selects a & b and i join them like:
select * from
(
select n.id_b || ',' || s.id_b ids, n.name, s.surname
from names n,
surnames s where n.id_a = s.id_a
) a
left join
(
select sn.id, sn.second_name
) b on b.id in (a.ids)
in this case join doesn't work :(
The problem is in b.id in (a.ids). But why if it looks like 12 in (12,24) and no result :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是行不通的,因为
(12,24)
是单个离散字符串,而不是逗号分隔的数字集。显然,12 != '(12,24)'
,因此没有返回结果。编辑
我错过了您发布的查询中的外部联接。因此,即使两个表之间没有连接,您也应该返回一些东西。以下是一些测试数据:
我的查询与您的查询类似,只是我将
b.id
转换为字符串,因为a.ids
是一个字符串。如果我不这样做,查询就会失败,并显示ORA-1722: invalid number
。正如您所看到的,它从左侧查询返回值,而从右侧查询不返回任何值,原因如我上面给出的。
如果您想根据 ID 的部分匹配从两个查询中获取某些内容,您需要执行以下操作:
如果您采用后一种方法,您可能需要考虑将外部连接转变为内部连接。取决于您精确的业务规则。
This won't work because
(12,24)
is a single discrete string not a comma-separated set of numbers. Clearly,12 != '(12,24)'
, hence no results are returned.edit
I missed the outer join in your posted query. So you ought to get something back, even though there is no join between the two tables. Here is some test data:
My query is similar to yours except I cast
b.id
as a string becausea.ids
is a string. If I don't do this, the query fails withORA-1722: invalid number
.As you can see, it returns values from the left-hand query and nothing from the right, for the reason which I gave above.
If you want to get something from both queries on the basis on partial matching of the IDs you need to do this:
If you take this latter approach you may want to consider turning the outer join into an inner. Depnds on you precise business rule.