搜索表格以查看它是否包含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。
简单联合
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))
这是DB2
中的记录存在测试SELECT
CASE COUNT(*)
WHEN 0 THEN 'FALSE'
ELSE 'TRUE'
END as IsRecordThere
FROM MYTABLE
WHERE COL1 = 'whatever' AND COL2 = 'etc'
我发现它可以与db2合作:
SELECT A, B,
Case C
WHEN 'Citron' THEN 'Lemon'
WHEN 'Charlie' THEN 'Name'
end as Choice,
D
FROM TABLE with UR;
也与DB2 Java驱动程序一起使用。