帮助处理左外连接与父子关系

发布于 2024-11-06 08:34:44 字数 362 浏览 6 评论 0原文

我的数据库中有下表:

id   grp#  code  parent#
--   ----  ----  -------
 0     10  US       NULL     
 0     30  SF         10 
 1     10  S        NULL 
 1     30  SF         10

根据此表,给定 id 和 grp#,我需要返回子项列表和 grp# 的代码。如果孩子不存在,则应返回 NULL。

例如:对于 id = 0,grp#= 10,它应该返回 US, 30;对于 id = 0,grp#=30,它应该返回 SF,NULL

注意:输出中不应有重复项。

i have following table in my database :

id   grp#  code  parent#
--   ----  ----  -------
 0     10  US       NULL     
 0     30  SF         10 
 1     10  S        NULL 
 1     30  SF         10

From this, given an id and grp# i need to return the list of children and the code for the grp#. If the children doesn't exist it should return NULL.

So for example : for id = 0, grp#= 10 it should return US, 30 and for id = 0, grp#=30 it should return SF,NULL

Note : there should not be duplicates in the output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

椵侞 2024-11-13 08:34:44

这是您的测试数据:

SQL> select * from t42
  2  /

        ID       GRP# CODE                    PARENT#
---------- ---------- -------------------- ----------
         0         10 US
         0         30 SF                           10
         1         10 S
         1         30 SF                           10

SQL>

这是一个返回您想要的结果的查询:

SQL> select p.code
  2         , c.grp# as child_grp#
  3  from t42 p
  4       left outer join t42 c
  5        on ( c.parent# = p.grp# )
  6  where p.id = &id
  7  and p.grp# = &grp
  8  /
Enter value for id: 0
old   6: where p.id = &id
new   6: where p.id = 0
Enter value for grp: 10
old   7: and p.grp# = &grp
new   7: and p.grp# = 10

CODE                 CHILD_GRP#
-------------------- ----------
US                           30
US                           30

SQL> r
  1  select p.code
  2         , c.grp# as child_grp#
  3  from t42 p
  4       left outer join t42 c
  5        on ( c.parent# = p.grp# )
  6  where p.id = &id
  7* and p.grp# = &grp
Enter value for id: 0
old   6: where p.id = &id
new   6: where p.id = 0
Enter value for grp: 30
old   7: and p.grp# = &grp
new   7: and p.grp# = 30

CODE                 CHILD_GRP#
-------------------- ----------
SF

SQL>

“我们可以重写查询以返回单行而不是
重复?”

当然 - 前提是您可以指定附加业务规则。

简单的方法是部署 DISTINCT 关键字,这是损坏查询的最后手段。

So here is your test data:

SQL> select * from t42
  2  /

        ID       GRP# CODE                    PARENT#
---------- ---------- -------------------- ----------
         0         10 US
         0         30 SF                           10
         1         10 S
         1         30 SF                           10

SQL>

And here is a query which returns the results you want:

SQL> select p.code
  2         , c.grp# as child_grp#
  3  from t42 p
  4       left outer join t42 c
  5        on ( c.parent# = p.grp# )
  6  where p.id = &id
  7  and p.grp# = &grp
  8  /
Enter value for id: 0
old   6: where p.id = &id
new   6: where p.id = 0
Enter value for grp: 10
old   7: and p.grp# = &grp
new   7: and p.grp# = 10

CODE                 CHILD_GRP#
-------------------- ----------
US                           30
US                           30

SQL> r
  1  select p.code
  2         , c.grp# as child_grp#
  3  from t42 p
  4       left outer join t42 c
  5        on ( c.parent# = p.grp# )
  6  where p.id = &id
  7* and p.grp# = &grp
Enter value for id: 0
old   6: where p.id = &id
new   6: where p.id = 0
Enter value for grp: 30
old   7: and p.grp# = &grp
new   7: and p.grp# = 30

CODE                 CHILD_GRP#
-------------------- ----------
SF

SQL>

"Can we rewrite the query to return a single row instead of
duplicates ?"

Certainly - provided you can specify the additional business rule.

The easy way out is to deploy the DISTINCT keyword, the last resort of the broken query.

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