我有以下选择
Select Exam.Name,
Test.Type,
Test.TestId,
Topic.Name,
Exam.Name
FROM Test
INNER JOIN Exam
ON ( Test.ExamId = Exam.ExamId)
INNER JOIN Topic
ON ( Test.TopicId = Topic.TopicId)
WHERE Test.TestStatusId = 1
当Test.Type = 1时,将有一个测试的Exam.Name。当Test.Type = 2时,将有一个测试的Topic.Name。
有没有一种方法可以进行外部联接,并根据Type = 1或2返回Name?
使用left join
和case
Select Test.Type,
Test.TestId,
case when Test.Type = 1 then Exam.Name
when Test.Type = 2 then Topic.Name
end as Name
FROM Test
LEFT JOIN Exam ON Test.ExamId = Exam.ExamId
LEFT JOIN Topic ON Test.TopicId = Topic.TopicId
WHERE Test.TestStatusId = 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句