【发布时间】:2019-03-08 19:13:39
【问题描述】:
我有一个表,其中包含一个名为 ReportTypeId 的列。我想选择 ReportTypeId = 1 的行,但如果不存在此行,那么我想要 ReportTypeId = 2。我正在尝试使用 WHEN EXISTS,但我无法弄清楚如何选择多于一列。我想编写一个看起来像这样的查询:
SELECT CASE
WHEN EXISTS (SELECT PerformanceReport FROM ReportData
WHERE (ReportId = 79 and ReportTypeId = 1))
THEN (select * from ReferenceData
where ReportTypeId = 1)
ELSE (select * from ReferenceData
where ReportTypeId = 2)
END
但是因为我试图返回不止一列,所以它不起作用。有没有办法根据数据是否存在来创建基于 WHERE 语句的查询?
【问题讨论】:
标签: sql select exists case-when