我在下面有 3 张桌子。(示例)(mySql 脚本)
顾客
- cust_id
- 客户名称
- 用户身份
- user_id_2
用户
- 用户身份
- em_id
员工
- em_id
- em_name
如何通过与连接同时交叉表调用em_name
字段user_id
和em_name
字段?user_id_2
user
我在下面有 3 张桌子。(示例)(mySql 脚本)
如何通过与连接同时交叉表调用em_name
字段user_id
和em_name
字段?user_id_2
user
尝试这个
SELECT q1.* ,
q2.em_name AS 'em_name_2'
FROM (SELECT c.cust_id ,
c.cust_name ,
c.user_id ,
c.user_id_2 ,
e.em_name
FROM dbo.customer AS c
INNER JOIN dbo.[user] AS u ON c.user_id = u.user_id
INNER JOIN dbo.employee AS e ON u.em_id = e.em_id
) q1
CROSS JOIN
( SELECT e.em_id ,
e.em_name
FROM dbo.customer AS c
INNER JOIN dbo.[user] AS u ON c.user_id_2 = u.user_id
INNER JOIN dbo.employee AS e ON u.em_id = e.em_id
) q2