带有2个INNER JOINS的SQL View语句

时间:2014-08-27 08:48:24

标签: sql sql-server view

任何人都可以帮助我解决这个问题,我曾多次尝试但仍然没有找到解决方案 这是我在数据库中的原始视图,但现在我在数据库中进行了更改,并且视图也需要更改。

以下是视图:

SELECT 
    [tableN].*, 
    [tabB].[att1] AS tabB_email, [tabB].[name] AS tabB_name,
    [tabC].[name] AS tabC_name
FROM  
    [tabC] 
INNER JOIN 
    ([tableN] 
INNER JOIN [tabB] ON [tableN].[sender_id] = [tabB].[ID]) 
     ON [tabC].[ID] = [tableN].[recipient_id]   

这对我来说有什么难点。现在我没有这两个表tabBtabC

它们现在位于一个表tabX中,并且具有标识符字段roleId。我设法获取除最后一列[tabC].[name] AS tabC_name

之外的所有列

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

  SELECT [tableN].*, [Tabx] .[att1] AS tabB_email, [Tabx] .[name] AS tabB_name  

  FROM  [Tabx] A 

  INNER JOIN [TABLEN] B

  ON A.ROLEID=B.RECIPIENT_ID

答案 1 :(得分:0)

试试这个

SELECT [tableN].*, [tabX].[att1] AS tabB_email, [tabX].[name] AS tabB_name, 
t1.[name] AS tabC_name 
FROM [tabX] as t INNER JOIN ([tableN] INNER JOIN [tabX] 
ON [tableN].[sender_id] = [tabX].[roleid]) 
ON t.[roleid] = [tableN].[recipient_id]

答案 2 :(得分:0)

 SELECT [TableN].*, Bd.[email] AS bd_email, Bd.[showname] AS bd_name,
        Pc.[showname] AS pc_name
   FROM 
      [TABLE_X] AS Pc 
         INNER JOIN ([TableN] 
         INNER JOIN [TABLE_X]  AS Bd
         ON [TableN].[sender_id] = Bd.[ID] AND Bd.roleID = 1) 
         ON Pc.[ID] = [TableN].[recipient_id] AND Pc.roleID = 2

我终于找到了根据需要运行的代码