CASE语句是否允许加入?有没有一个好方法来完成这里的任务?我的真实查询还有其他一些左连接。
我想在这种情况下加入T1和T2: 1.当T1.sub_service不为null时,则加入T2.type 2.当T1.sub_service为null时,使用T1.service与T2.type
连接SELECT T1.service, T1.sub_service, T2.type
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
ON T2.type LIKE
CASE WHEN T1.sub_service IS NULL THEN T1.service
WHEN T1.sub_service IS NOT NULL THEN T1.sub_service
END
答案 0 :(得分:3)
只需将 LIKE 替换为 = :
ON T2.type =
CASE WHEN T1.sub_service IS NULL THEN T1.service
WHEN T1.sub_service IS NOT NULL THEN T1.sub_service
END
但你可以进一步将其简化为COALESCE:
ON T2.type = COALESCE(T1.sub_service, T1.service)