我有表格的数据库..
war {
if (project.hasProperty('node')) {
classpath fileTree("stacks/${project.node}")
}
}
查询是..
A
idA | name
-----------------
1 | A
B
idB | idA | name
-----------------------
2 | 1 | AA
3 | 1 | BB
4 | 1 | CC
C
idC | idA | name
-----------------------
6 | 1 | AAA
7 | 1 | BBB
然后结果是..
select
A.*,
B.idB, B.name,
C.idC, C.name
from
A
join
B on B.idA=A.idA
join
C on C.idA=A.idA
我需要这样的结果..
idA | name | idB | name | idC | name
----------------------------------------------------
1 | A | 2 | AA | 6 | AAA
1 | A | 2 | AA | 7 | BBB
1 | A | 3 | BB | 6 | AAA
1 | A | 3 | BB | 7 | BBB
1 | A | 4 | CC | 6 | AAA
1 | A | 4 | CC | 7 | BBB
每个相同或重复的值/ id,将使用null或空值更改/替换..
有人可以帮忙吗?
答案 0 :(得分:1)
要解决此问题,您需要在表之间使用“join”键。实际上,解决方案(在MySQL中)不会使用join
,但您需要为每个解决方案指定行号:
select (case when rn = 1 then ida end) as ida,
(case when rn = 1 then namea end) as namea,
max(idb) as idb, max(nameb) as nameb,
max(idc) as idc, max(namec) as namec
from ((select a.idA, a.name as namea, b.id as idb, b.name as nameb,
NULL as idc, NULL as namec,
(@rnb := @rnb + 1) as rn
from a join
b
on b.ida = a.id
) union all
(select a.idA, a.name, NULL, NULL,
c.id as idc, c.name as namec,
(@rnc := @rnc + 1) as rn
from a join
c
on c.idc = a.id
)
) abc
group by ida, namea, rn;
union all
/ group by
实际上是在实现full outer join
(在这种情况下),MySQL不支持。
答案 1 :(得分:0)
表格模型可能是这样的..
A
idA | name
-----------------
1 | A
B
idB | idA | name
-----------------------
2 | 1 | AA
3 | 1 | BB
5 | 1 | CC
6 | 1 | DD
7 | 1 | EE
C
idC | idA | name
-----------------------
8 | 1 | AAA
9 | 1 | BBB
表B可以是可变值,表C是静态值..
答案 2 :(得分:0)
然后结果是..
idA | name | idB | name | idC | name
----------------------------------------------------
1 | A | 2 | AA | 8 | AAA
1 | A | 2 | AA | 9 | BBB
1 | A | 3 | BB | 8 | AAA
1 | A | 3 | BB | 9 | BBB
1 | A | 5 | CC | 8 | AAA
1 | A | 5 | CC | 9 | BBB
1 | A | 6 | DD | 8 | AAA
1 | A | 6 | DD | 9 | BBB
1 | A | 7 | EE | 8 | AAA
1 | A | 7 | EE | 9 | BBB
我需要这样的结果..
idA | name | idB | name | idC | name
----------------------------------------------------
1 | A | 2 | AA | 8 | AAA
| | 3 | BB | 9 | BBB
| | 5 | CC | |
| | 6 | DD | |
| | 7 | EE | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |