在我的系统中有三层
BDM can add Agent
DSA can add Agent
Agent can add customer
在代理中有两列agent_bdm_id和agent_dsa_id 如果通过BDM添加代理,我将存储零,反之亦然
我需要使用代理和bde / dsa名称打印客户
BDM bdmid, bdmname
DSA dsaid, dsaname
Agent agent_id, agent_name, agent_bdm_id, agent_dsa_id
customer cust_is, cust_name, cust_agent_id
答案 0 :(得分:1)
SELECT a.cust_is, a.cust_name,
b.agent_name,
COALESCE(c.bdmname, d.dsaname) `BDM/DSA`
FROM customer a
INNER JOIN Agent b
ON a.cust_agent_id = b.agent_id
LEFT JOIN BDM c
ON b.agent_bdm_id = c.bdmid
LEFT JOIN DSA d
ON b.agent_dsa_id = d.dsaid
我有点困惑。为什么0
?是ID
添加了Agent
?