SQL多个连接

时间:2011-01-25 14:27:39

标签: sql sql-server

鉴于以下两个表:

CREATE TABLE [dbo].[MTCorrelations]
(
    [CorrelationID] [int] IDENTITY(1,1) NOT NULL,
    [StockA] [nvarchar](5) NOT NULL,
    [StockB] [nvarchar](5) NOT NULL,
    [Correlation] [float] NOT NULL,
    [LengthStr] [nvarchar](5) NOT NULL,
    [Date] [datetime] NOT NULL
)

CREATE TABLE [dbo].[Industries]
(
    [IndustryID] [int] IDENTITY(1,1) NOT NULL,
    [Symbol] [nvarchar](5) NOT NULL,
    [Sector] [nvarchar](50) NULL,
    [Industry] [nvarchar](50) NULL
)

我正试图从Industries表中查找StockA和StockB的行业。但是,我不知道如何进行多个连接。这是我能想到的最好的:

SELECT TOP 1000 
[CorrelationID]

      ,[StockA]
      ,[StockB]
      ,[Correlation]
      ,b.Industry
      ,c.Industry
  FROM [MarketTopology].[dbo].[MTCorrelations] as a JOIN [MarketTopology].[dbo].[Industries] as b ON a.StockA = b.Symbol
  AND a JOIN [MarketTopology].[dbo].[Industries] as c ON a.StockB = c.Symbol

我在AND上收到错误。这样做的正确方法是什么?

6 个答案:

答案 0 :(得分:3)

SELECT  TOP 1000 
        [CorrelationID]
       ,[StockA]
       ,[StockB]
       ,[Correlation]
       ,b.Industry
       ,c.Industry
FROM   [MarketTopology].[dbo].[MTCorrelations] AS a
JOIN   [MarketTopology].[dbo].[Industries] AS b
ON     b.Symbol = a.StockA
JOIN   [MarketTopology].[dbo].[Industries] AS c
ON     c.Symbol = a.StockB

答案 1 :(得分:2)

删除AND a,然后获取下一个JOIN

SELECT TOP 1000  
          [CorrelationID],
          [StockA],
          [StockB],
          [Correlation],
          b.Industry,
          c.Industry   
  FROM [MarketTopology].[dbo].[MTCorrelations] AS a
  JOIN [MarketTopology].[dbo].[Industries] AS b 
    ON a.StockA = b.Symbol 
  JOIN [MarketTopology].[dbo].[Industries] AS c 
    ON a.StockB = c.Symbol

答案 2 :(得分:2)

您可以使用:

SELECT TOP 1000 
      [CorrelationID],
      [StockA],
      [StockB],
      [Correlation],
      b.Industry,
      c.Industry
 FROM [MarketTopology].[dbo].[MTCorrelations] as a 
           JOIN [MarketTopology].[dbo].[Industries] as b ON a.StockA = b.Symbol 
           JOIN [MarketTopology].[dbo].[Industries] as c ON a.StockB = c.Symbol

答案 3 :(得分:2)

试试这个

SELECT TOP 1000 
    [CorrelationID]
    ,[StockA]
    ,[StockB]
    ,[Correlation]
    ,b.Industry
    ,c.Industry
FROM 
    [MarketTopology].[dbo].[MTCorrelations] as a 
    INNER JOIN [MarketTopology].[dbo].[Industries] as b 
        ON a.StockA = b.Symbol
    INNER JOIN [MarketTopology].[dbo].[Industries] as c 
        ON a.StockB = c.Symbol

另外,IMO你应该放弃使用A,B,C别名约定并给你的表别名意味着什么。这样,无论您正在查看哪个查询,Ind的{​​{1}}可能总是缩短形式。

答案 4 :(得分:0)

第二次加入之前无需AND a

SELECT TOP 1000 [CorrelationID]
               ,[StockA]
               ,[StockB]
               ,[Correlation]
               ,b.Industry
               ,c.Industry
    FROM [MarketTopology].[dbo].[MTCorrelations] as a 
        JOIN [MarketTopology].[dbo].[Industries] as b 
            ON a.StockA = b.Symbol
        JOIN [MarketTopology].[dbo].[Industries] as c 
            ON a.StockB = c.Symbol

答案 5 :(得分:0)

您的查询有拼写错误。将其更改为:

SELECT TOP 1000 
[CorrelationID]

      ,[StockA]
      ,[StockB]
      ,[Correlation]
      ,b.Industry
      ,c.Industry
  FROM [MarketTopology].[dbo].[MTCorrelations] as a JOIN [MarketTopology].[dbo].[Industries] as b ON a.StockA = b.Symbol
   JOIN [MarketTopology].[dbo].[Industries] as c ON a.StockB = c.Symbol