表A,表B一对多关系,但要查询一对一的数据
表Question(id,question)
表Answer(QuestionID,Answer,isBest)
一条Question可以有多条Answer,但是IsBest为1的数据只能一条,
现要查询一条Question对应一条Answer,(如果Answer有Isbest =1 的就选,没有就选第一条,没有数据就为空) SqlServer
[解决办法]
try this,
select a.id,a.question,isnull(b.Answer,'') 'Answer'
from Question a
left join
(select * from Answer where isBest=1) b on a.id=b.QuestionID