如何在3个表上执行SQL JOIN?

时间:2013-04-17 20:47:20

标签: mysql sql

我正在使用MySQL,我试图从三个不同的表中获取数据,但我不确定我应该使用的语法。

我有一个名为full_daily_data_1的表,其中包含以下字段 符号 Trade_Date Day_Open Day_High

我还有一个名为custom_indices_xref的表,其中包含以下字段: 符号 Custom_Index_Name

还有一个名为daily_index_weightings的表,其中包含以下字段: Custom_Index_Name 符号 Trade_date combo_weighting

现在,我将它作为select语句从两个表中获取所需的数据:

SELECT 
    Symbol, 
    Trade_Date, 
    Day_Open, 
    Day_High
FROM 
    full_daily_data_1 
WHERE 
    trade_date >= '2012/01/01' AND
    trade_date <= '2012/01/31' AND 
    symbol in (SELECT symbol from custom_indices_xref WHERE Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks');

但我想要的是日期范围内每个日期的每个符号的以下数据 符号 Day_Open Day_High, Custom_Index_Name 符号 Trade_date combo_weighting

所以基本上,需要在daily_index_weightings表中添加所选日期和符号的combo_weighting。我的SQL语句应该如何实现呢?

我尝试了这个,但是我收到了SQL语法错误,所以不确定我做错了什么:

SELECT 
    full_daily_data_1.Symbol, 
    full_daily_data_1.Trade_Date,
    full_daily_data_1.Day_Open, 
    full_daily_data_1.Day_High,
    full_daily_data_1.Day_Low,
    daily_index_weightings.combo_weighting
FROM 
    full_daily_data_1
WHERE 
    trade_date >= '2012/01/01' AND  
    trade_date <= '2012/01/31' AND 
    Symbol in (SELECT symbol from custom_indices_xref WHERE Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks')
JOIN 
    daily_index_weightings ON 
        daily_index_weightings.symbol = full_daily_data_1.Symbol AND 
        daily_index_weightings.Trade_Date = full_daily_data_1.Trade_Date ; 

2 个答案:

答案 0 :(得分:0)

 SELECT fdd.Symbol, fdd.Trade_Date, fdd.Day_Open, fdd.Day_High, fdd.Day_Low, diw.combo_weighting 
 FROM full_daily_data_1 fdd 
 INNER JOIN custom_indicies_xref cix ON fdd.symbol=cix.symbol 
 INNER JOIN daily_index_weighings diw ON fdd.symbol = diw.symbol
 WHERE 
 trade_date >= '2012/01/01' AND  
 trade_date <= '2012/01/31' AND 
 cix.Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks'

如果需要,INNER JOINS可以更改为LEFT JOIN。

或者,因为你的第二个和第三个表也匹配Custom_Index_Name字段(顺便说一句,这是糟糕的设计):

 SELECT fdd.Symbol, fdd.Trade_Date, fdd.Day_Open, fdd.Day_High, fdd.Day_Low, diw.combo_weighting  
 FROM full_daily_data_1 fdd 
 INNER JOIN (custom_indicies_xref cix ON fdd.symbol=cix.symbol 
      INNER JOIN daily_index_weighings diw ON cix.Custom_Index_Name = diw.Custom_Index_Name)
 ON fdd.symbol = cix.symbol
 WHERE 
 trade_date >= '2012/01/01' AND  
 trade_date <= '2012/01/31' AND 
 cix.Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks'

答案 1 :(得分:0)

你的sql子句乱序,你应该为你的表名使用别名,只是让它更容易阅读。我注意到的另一件事是你的WHERE子句中的字段是不明确的。

SELECT 
    FDD.Symbol, 
    FDD.Trade_Date,
    FDD.Day_Open, 
    FDD.Day_High,
    FDD.Day_Low,
    DIW.combo_weighting
FROM 
    full_daily_data_1 FDD
JOIN 
    daily_index_weightings DIW ON 
        DIW.symbol = FDD.Symbol AND 
        DIW.Trade_Date = FDD.Trade_Date ; 
WHERE 
    FDD.trade_date >= '2012/01/01' AND  
    FDD.trade_date <= '2012/01/31' AND 
    FDD.Symbol in 
    (SELECT symbol from custom_indices_xref WHERE Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks')