我有以下问题。我正在尝试加入三个表:
TableA:Record_id,Reference_id,Param
表B:Doc_id,NameB
TableC:Doc_id,NameC
取决于TableA.Param中的值,我需要通过加入Reference_id来选择表B或C中的Name
我试过使用“case”约束,但没有解决:)
select a.Record_id, a.Reference_id
, case when Param = 'B' then b.NameB
when Param = 'C' then c.NameC
from TableA as a inner join
TableB as b on a.Reference_id = b.Doc_id inner join
TableC as c on a.Reference_id = c.Doc_id
有什么想法吗?
答案 0 :(得分:0)
您可以left join
使用该条件并使用coalesce()
select a.Record_id, a.Reference_id,
coalesce(b.NameB, c.NameC) as name
from TableA as a
left join TableB as b on a.Reference_id = b.Doc_id
and Param = 'B'
and b.STAT = 'A'
left join TableC as c on a.Reference_id = c.Doc_id
and Param = 'C'
and c.STAT = 'A'
BTW您的查询已关闭(错过了end
)。这适用于left join
s
select a.Record_id, a.Reference_id,
case when Param = 'B' then b.NameB
when Param = 'C' then c.NameC
end as name
from TableA as a
left join TableB as b on a.Reference_id = b.Doc_id and b.STAT = 'A'
left join TableC as c on a.Reference_id = c.Doc_id and c.STAT = 'A'
答案 1 :(得分:0)
试试这个:
SELECT a.Record_id, a.Reference_id, b.NameB AS Name
FROM TableA AS a
INNER JOIN TableB AS b ON a.Reference_id = b.Doc_id
WHERE a.Param = 'B'
UNION
SELECT a.Record_id, a.Reference_id, c.NameC AS Name
FROM TableA AS a
INNER JOIN TableC as c ON a.Reference_id = c.Doc_id
WHERE c.Param = 'C'
答案 2 :(得分:0)
Ext.Inventory.prototype.addAlias = function(className,alias, update) {
return this.addMapping(className, alias, this.aliasToName, this.nameToAliases, true);
};