我有5个表A,B,C,D,E。
A的主键是B,C,D,E中的外键。它们没有自己的主键。
我必须在其中实现某种逻辑
if ("A JOIN B JOIN C" IS NOT NULL)
return result of "A JOIN B JOIN C"
else if ("A JOIN C" IS NOT NULL)
return result of "A JOIN C"
else if ("A JOIN D" IS NOT NULL)
return result of "A JOIN D"
else if ("A JOIN E" IS NOT NULL)
return result of "A JOIN E"
else return A
where A.primarykey = <some value>
我必须在单个sql中而不是在存储的proc中实现它。 我想不出一种方法来实现这一目标。我使用DB2。 如果您能就如何进行操作提供一些指导,我将不胜感激。
答案 0 :(得分:2)
您可以执行以下操作:
select
case
when B.key is not null and C.key is not null then 'A+B+C'
when C.key is not null then 'A+C'
when D.key is not null then 'A+D'
when E.key is not null then 'A+E'
end
from A
left join B on B.key = A.key
left join C on C.key = A.key
left join D on D.key = A.key
left join E on E.key = A.key
left join F on F.key = A.key
一旦满足case
的条件,when
语句将停止进一步评估,因此实际上,您有一个if-else if
块。
我只是在示例中返回字符串值,但是您也可以根据需要返回字段值,例如:
case
when B.key is not null and C.key is not null then b.f1
when C.key is not null then c.f1
when D.key is not null then d.f1
when E.key is not null then e.f1
end as 'f1'
,
case
when B.key is not null and C.key is not null then b.f2
when C.key is not null then c.f2
when D.key is not null then d.f2
when E.key is not null then e.f2
end as 'f2'
答案 1 :(得分:1)
您希望LEFT JOIN
带有一些其他逻辑来处理您正在做的事情的“其他”部分:
select . . .
from A left join
B
on B.key = A.key left join
C
on C.key = A.key left join
C c2
on c2.key = A.key and
c.key is null left join -- first attempt failed
D
on D.key = A.key and
c.key is null and
c2.key is null left join -- earlier joins failed
E
on e.key = A.key and
c.key is null and
c2.key is null and
d.key is null
答案 2 :(得分:1)
尝试以下方法。您可以编辑输入数据以检查结果。
WITH
A (ID, AV) AS (VALUES (1, 'A1'))
, B (ID, BV) AS (VALUES
(1, 'B1'), (1, 'B2')
--(2, 'B1'), (2, 'B2')
) , C (ID, CV) AS (VALUES
(1, 'C1'), (1, 'C2'), (1, 'C3')
--(2, 'C1'), (2, 'C2'), (2, 'C3')
), D (ID, DV) AS (VALUES
(1, 'D1'), (1, 'D2')
--(2, 'D1'), (2, 'D2')
) , E (ID, EV) AS (VALUES
(1, 'E1'), (1, 'E2'), (1, 'E3')
--(2, 'E1'), (2, 'E2'), (2, 'E3')
)
SELECT A.ID, A.AV, B.BV, C.CV, C1.CV AS CV1, D.DV, E.EV
,
CASE
WHEN B.ID IS NOT NULL THEN 'A+B+C'
WHEN C1.ID IS NOT NULL THEN 'A+C'
WHEN D.ID IS NOT NULL THEN 'A+D'
WHEN E.ID IS NOT NULL THEN 'A+E'
ELSE 'A'
END JOIN_PATH
FROM A
LEFT JOIN
(
B
JOIN C ON C.ID=B.ID
) ON B.ID = A.ID
LEFT JOIN C C1 ON C1.ID = A.ID AND B.ID IS NULL
LEFT JOIN D ON D.ID = A.ID AND B.ID IS NULL AND C1.ID IS NULL
LEFT JOIN E ON E.ID = A.ID AND B.ID IS NULL AND C1.ID IS NULL AND D.ID IS NULL;