同时获得2个相同的字段

时间:2013-06-21 06:26:48

标签: mysql inner-join

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

客户

  • 的cust_id
  • CUST_NAME
  • USER_ID
  • user_id_2

用户

  • USER_ID
  • em_id

雇员

  • em_id
  • em_name

如何通过与同时跨越em_name表格来调用user_id的{​​{1}}字段和em_name的{​​{1}}字段加入 ...

1 个答案:

答案 0 :(得分: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