【发布时间】:2019-11-17 22:07:10
【问题描述】:
我们必须根据表 1 列 itemtype 连接 4 个表。
表A包含id,itemtype,itemid
表 B 包含 id、全名
表 c 包含 id、全名
表D包含id、全名
表 A.itemid 在(表 B 或表 C 或表 D)的 id 中。
我想将表 A 与剩余表连接起来以获得全名。根据 TableA 列 itemtype 值加入另一个表
select
tblA.itemid,y.fullname from tableA as tblA
inner join (
CASE WHEN tblA.itemtype = 1 THEN
select
tblB.itemid as id,tblB.fullname as fullname
from
tableB as tblB
where
tblB.id = tblA.itemid
WHEN tblA.itemtype = 2 THEN
select
tblC.itemid as id,tblC.fullname as fullname
from
tableC as tblC
where
tblC.id = tblA.itemid
WHEN tblA.itemtype = 3 THEN
select
tblD.itemid as id ,tblD.fullname as fullname
from
tableD as tblD
where
tblD.id = tblA.itemid
END
) as bcd on bcd.id = tblA.itemid
【问题讨论】:
标签: mysql