我的桌子
革新信息

BlockCode|arsehRow|SakhtemanRow
-------------------------------
1        |1       |00
2        |2       |00
3        |3       |01

阿尔塞马尔克
BlockCode|arsehRow|name
------------------------
1        |1       |a
2        |2       |b

阿帕特曼马尔克
BlockCode|arsehRow|SakhtemanRow|name
-------------------------------------------------
3        |3       |01          |d

我想加入Renovationinfo如果ArsehMalk
如果SakhtemanRow=='00'
我编写了这个查询,但它不返回任何内容。
Select name
From
    Renovationinfo r
Left Join
     ArsehMalk m1 On m1.BlockCode = r.BlockCode And
     m1.arsehRow = r.arsehRow And r.SakhtemanRow = '00'
Left Join
     ApartmanMalk m2 On m2.BlockCode = r.BlockCode And
     m2.arsehRow = r.arsehRow And
     m2.SakhtemanRow=r.SakhtemanRow  And
     r.SakhtemanRow<>'00'

我想要这个输出
BlockCode|arsehRow|name
-------------------------------
1        |1       |a
2        |2       |b
3        |3       |d

最佳答案

您的查询有一个基本问题:name不明确。您可以将其修复为:

select coalesce(m1.name, m2.name) as name

因为您需要其他列:
select r.BlockCode, r.arehRow, coalesce(m1.name, m2.name) as name

10-02 02:50
查看更多