如何在 SQL 查询的 SELECT 子句中嵌入一个嵌套的 IF 语句?我知道可以使用 case when condition then X else y end,但是如何以同样的方式为记录集中的每个记录执行嵌套的 IF 语句。
if x.boy is not null then
x.boy
else if x.girl is not null
then x.girl
else if x.dog is not null
then x.dog
else
x.cat
这是我的尝试:
SELECT top 10
id,
case when x.boy <> NULL then
x.boy
else case when x.girl <> NULL
x.girl
else case when x.dog <> NULL
x.dog
else x.cat
end as Who
from house x
这个是否正确?