多次加倍 JOIN 到同一个 table
Multiple doubled JOIN to the same table
示例 1(工作正常,选择 assignedagent):
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
u.userlastname as assignedagentname,
"" as answeringagentname from callrecord cr
left join (agentrecord ar left join users u on ar.agentkey=u.userkey)
on (cr.callid=ar.callid and cr.assignedagent=ar.sequencenumber);
示例 2(工作正常,选择 answeringagent):
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
"" as assignedagentname,
u.userlastname as answeringagentname from callrecord cr
left join (agentrecord ar left join users u on ar.agentkey=u.userkey)
on (cr.callid=ar.callid and cr.answeringagent=ar.sequencenumber);
示例 3(产生错误,同时选择两者):
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
u1.userlastname as assignedagentname,
u2.userlastname as answeringagentname from callrecord cr
left join (agentrecord ar left join users u1 on ar.agentkey=u1.userkey)
on (cr.callid=ar.callid and cr.assignedagent=ar.sequencenumber)
left join (agentrecord ar left join users u2 on ar.agentkey=u2.userkey)
on (cr.callid=ar.callid and cr.answeringagent=ar.sequencenumber);
示例 1 和示例 2 工作正常,returns 结果正确。在示例 3 中,我遵循了有关在该论坛中找到的多个连接的别名的建议,但没有成功。我通过 ODBC 驱动程序使用 Informix DB。
尝试删除括号并确保所有别名都是唯一的:
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
u1.userlastname as assignedagentname,
u2.userlastname as answeringagentname
from callrecord cr left join
agentrecord ar1
on cr.callid = ar1.callid and
cr.assignedagent = ar1.sequencenumber left join
users u1
on ar.agentkey = u1.userkey left join
agentrecord ar2
on cr.callid = ar2.callid and
cr.answeringagent = ar2.sequencenumber left join
users u2 on ar2.agentkey = u2.userkey
示例 1(工作正常,选择 assignedagent):
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
u.userlastname as assignedagentname,
"" as answeringagentname from callrecord cr
left join (agentrecord ar left join users u on ar.agentkey=u.userkey)
on (cr.callid=ar.callid and cr.assignedagent=ar.sequencenumber);
示例 2(工作正常,选择 answeringagent):
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
"" as assignedagentname,
u.userlastname as answeringagentname from callrecord cr
left join (agentrecord ar left join users u on ar.agentkey=u.userkey)
on (cr.callid=ar.callid and cr.answeringagent=ar.sequencenumber);
示例 3(产生错误,同时选择两者):
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
u1.userlastname as assignedagentname,
u2.userlastname as answeringagentname from callrecord cr
left join (agentrecord ar left join users u1 on ar.agentkey=u1.userkey)
on (cr.callid=ar.callid and cr.assignedagent=ar.sequencenumber)
left join (agentrecord ar left join users u2 on ar.agentkey=u2.userkey)
on (cr.callid=ar.callid and cr.answeringagent=ar.sequencenumber);
示例 1 和示例 2 工作正常,returns 结果正确。在示例 3 中,我遵循了有关在该论坛中找到的多个连接的别名的建议,但没有成功。我通过 ODBC 驱动程序使用 Informix DB。
尝试删除括号并确保所有别名都是唯一的:
select cr.callid, cr.callstart, cr.callend, cr.waittime, cr.origin,
cr.origdestination, cr.waitresolution, cr.contacttype, cr.termtype,
u1.userlastname as assignedagentname,
u2.userlastname as answeringagentname
from callrecord cr left join
agentrecord ar1
on cr.callid = ar1.callid and
cr.assignedagent = ar1.sequencenumber left join
users u1
on ar.agentkey = u1.userkey left join
agentrecord ar2
on cr.callid = ar2.callid and
cr.answeringagent = ar2.sequencenumber left join
users u2 on ar2.agentkey = u2.userkey