帮助处理左外连接与父子关系
我的数据库中有下表:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您的测试数据:
这是一个返回您想要的结果的查询:
当然 - 前提是您可以指定附加业务规则。
简单的方法是部署 DISTINCT 关键字,这是损坏查询的最后手段。
So here is your test data:
And here is a query which returns the results you want:
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.