我有一个简单的要求,但无法正确获得所需的输出。
我有两张桌子 - 父母,孩子
父表中的我有父母列表
P1
P2
P3
etc
在Child表中,我有
之类的记录P1 | P1_C1
P1 | P1_C2
P2 | P2_C1
P2 | P2_C3
P3 | P3_C4
etc
我需要编写一个应该返回以下内容的查询
第一行中的父记录,后跟其子记录,另一个父记录,后跟其子记录等
示例:
------ | ------
P1 | null
null | P1_C1
null | P1_C2
P2 | null
null | P2_C1
null | P2_C3
P3 | nul
null | P3_C4
答案 0 :(得分:1)
从两个表中选择父级和子级(如果可用)。然后按父级和子级排序(首先使用null子级)。您还需要一个案例表达式来在您展示孩子时压制父母。
@RunWith(AndroidJUnit4.class)
答案 1 :(得分:0)
请考虑这个解决方案。您不应该使用第一列,它仅用于正确排序:
select
pa.id, null, ch.value
from
parent pa
join child ch on parent.id=child.id
union
select
pa2.id, pa2.id, null
from
parent pa2
order by 1, 3 nulls first;