0

我在下面有 3 张桌子。(示例)(mySql 脚本)

顾客

  • cust_id
  • 客户名称
  • 用户身份
  • user_id_2

用户

  • 用户身份
  • em_id

员工

  • em_id
  • em_name

如何通过与连接同时交叉表调用em_name字段user_idem_name字段?user_id_2user

4

1 回答 1

1

尝试这个

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
于 2013-06-21T06:33:15.123 回答