存在时的DB2案例

时间:2016-12-22 15:37:00

标签: null db2 case exists

搜索表格,看它是否包含X,如果它找不到X我需要它返回Y.这是我到目前为止所尝试的:

SELECT WHEN T1.X IS NULL THEN Y ELSE T1.X END AS XY 
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.X = T2.X
WHERE T1.X IN (A, B, C);

SELECT CASE WHEN T1.X IS NOT NULL THEN T1.X ELSE Y END AS XY
FROM TABLE1 T1
WHERE T1.X IN (A, B, C)
AND T1.X IN (
    SELECT T2.X
    FROM TABLE2 T2);

SELECT CASE WHEN EXISTS (SELECT X FROM TABLE1) THEN X ELSE Y END AS XY
FROM TABLE1 
WHERE X IN (A, B, C);

我返回0行,它找不到A,B或C,而不是Y.但如果它找到A,B或C,它会正确返回X.

2 个答案:

答案 0 :(得分:0)

简单UNION

SELECT X FROM TABLE1 WHERE X IN (A, B, C) 
UNION 
SELECT Y FROM SYSIBM.SYSDUMMY1 
WHERE NOT EXISTS (SELECT X FROM TABLE1 WHERE X IN (A, B, C))

答案 1 :(得分:0)

我发现这可以与DB2一起使用:

SELECT A, B, 
Case C
WHEN 'Citron' THEN 'Lemon'
WHEN 'Charlie' THEN 'Name'
end as Choice, 
D
FROM TABLE with UR;

也可以使用DB2 Java驱动程序。