使用“in”在加入

发布于 2024-08-31 10:03:54 字数 363 浏览 2 评论 0原文

我有两个选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-09-07 10:03:54

这是行不通的,因为 (12,24) 是单个离散字符串,而不是逗号分隔的数字集。显然,12 != '(12,24)',因此没有返回结果。

编辑

我错过了您发布的查询中的外部联接。因此,即使两个表之间没有连接,您也应该返回一些东西。以下是一些测试数据:

SQL> select * from names
  2  /

      ID_A       ID_B NAME
---------- ---------- ----------
         1         12 SAM

SQL> select * from surnames
  2  /

      ID_A       ID_B SURNAME
---------- ---------- ----------
         1         24 I-AM

SQL> select * from whatever
  2  /

        ID SECOND_NAM
---------- ----------
        24 I AM SAM

SQL>

我的查询与您的查询类似,只是我将 b.id 转换为字符串,因为 a.ids 是一个字符串。如果我不这样做,查询就会失败,并显示 ORA-1722: invalid number

SQL> select * from
  2      (
  3           select n.id_b || ',' || s.id_b ids, n.name, s.surname
  4           from names n,
  5                surnames s
  6           where n.id_a = s.id_a
  7      ) a
  8  left join
  9      (
 10          select sn.id, sn.second_name
 11          from whatever sn
 12      ) b on to_char(b.id) in (a.ids)
 13  /

IDS        NAME       SURNAME            ID SECOND_NAM
---------- ---------- ---------- ---------- ----------
12,24      SAM        I-AM

SQL>

正如您所看到的,它从左侧查询返回值,而从右侧查询不返回任何值,原因如我上面给出的。

如果您想根据 ID 的部分匹配从两个查询中获取某些内容,您需要执行以下操作:

SQL> select a.ids
  2         , a.name
  3         , a.surname
  4         , b.id
  5         , b.second_name
  6  from
  7      (
  8           select n.id_b || ',' || s.id_b ids
  9                  , n.name
 10                  , s.surname
 11                  , n.id_b as n_id_b
 12                  , s.id_b as s_id_b
 13           from names n,
 14                surnames s
 15           where n.id_a = s.id_a
 16      ) a
 17  left join
 18      (
 19          select sn.id, sn.second_name
 20          from whatever sn
 21      ) b on (b.id = a.n_id_b or b.id = a.s_id_b )
 22  /

IDS        NAME       SURNAME            ID SECOND_NAM
---------- ---------- ---------- ---------- ----------
12,24      SAM        I-AM               24 I AM SAM

SQL>

如果您采用后一种方法,您可能需要考虑将外部连接转变为内部连接。取决于您精确的业务规则。

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:

SQL> select * from names
  2  /

      ID_A       ID_B NAME
---------- ---------- ----------
         1         12 SAM

SQL> select * from surnames
  2  /

      ID_A       ID_B SURNAME
---------- ---------- ----------
         1         24 I-AM

SQL> select * from whatever
  2  /

        ID SECOND_NAM
---------- ----------
        24 I AM SAM

SQL>

My query is similar to yours except I cast b.id as a string because a.ids is a string. If I don't do this, the query fails with ORA-1722: invalid number.

SQL> select * from
  2      (
  3           select n.id_b || ',' || s.id_b ids, n.name, s.surname
  4           from names n,
  5                surnames s
  6           where n.id_a = s.id_a
  7      ) a
  8  left join
  9      (
 10          select sn.id, sn.second_name
 11          from whatever sn
 12      ) b on to_char(b.id) in (a.ids)
 13  /

IDS        NAME       SURNAME            ID SECOND_NAM
---------- ---------- ---------- ---------- ----------
12,24      SAM        I-AM

SQL>

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:

SQL> select a.ids
  2         , a.name
  3         , a.surname
  4         , b.id
  5         , b.second_name
  6  from
  7      (
  8           select n.id_b || ',' || s.id_b ids
  9                  , n.name
 10                  , s.surname
 11                  , n.id_b as n_id_b
 12                  , s.id_b as s_id_b
 13           from names n,
 14                surnames s
 15           where n.id_a = s.id_a
 16      ) a
 17  left join
 18      (
 19          select sn.id, sn.second_name
 20          from whatever sn
 21      ) b on (b.id = a.n_id_b or b.id = a.s_id_b )
 22  /

IDS        NAME       SURNAME            ID SECOND_NAM
---------- ---------- ---------- ---------- ----------
12,24      SAM        I-AM               24 I AM SAM

SQL>

If you take this latter approach you may want to consider turning the outer join into an inner. Depnds on you precise business rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文