简体   繁体   English

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

[英]How do I do a SQL JOIN on 3 tables?

I am using MySQL and I am trying to get data out of three different tables but am not sure of the syntax that I should use. 我正在使用MySQL,并且试图从三个不同的表中获取数据,但是不确定应该使用的语法。

I have a table called full_daily_data_1 with the following fields Symbol Trade_Date Day_Open Day_High 我有一个名为full_daily_data_1的表,其中包含以下字段Symbol Trade_Date Day_Open Day_High

I also have a table called custom_indices_xref with the following fields: symbol Custom_Index_Name 我还有一个名为custom_indices_xref的表,其中包含以下字段:符号Custom_Index_Name

And a table called daily_index_weightings with the following fields: Custom_Index_Name Symbol Trade_date combo_weighting 还有一个名为daily_index_weightings的表,其中包含以下字段:Custom_Index_Name符号Trade_date combo_weighting

Right now, I have this as a select statement to get the data I need out of two tables: 现在,我将其作为一条选择语句从两个表中获取所需的数据:

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');

But what I want is the following data for each symbol for each date in the date range symbol Day_Open Day_High, Custom_Index_Name Symbol Trade_date combo_weighting 但是我想要的是日期范围符号Day_Open Day_High,Custom_Index_Name符号Trade_date combo_weighting中每个日期的每个符号的以下数据

So basically, needing to add combo_weighting from the daily_index_weightings table for the selected days and symbols. 因此,基本上,需要为所选的日期和符号从daily_index_weightings表添加combo_weighting。 What should my SQL statement look like to accomplish this? 我的SQL语句应如何完成此任务?

I tried this, but I get a SQL syntax error, so not sure what I'm doing wrong: 我试过了,但是出现了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 ; 
 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'

The INNER JOINS could be changed to LEFT JOIN if required. 如果需要,可以将INNER JOINS更改为LEFT JOIN。

Alternatively, since your second and third tables also match the Custom_Index_Name fields (which is bad design, by the way): 或者,由于您的第二个和第三个表也与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'

Your sql clauses are out of order and you should use alias for your table names, just makes it easier to read. 您的sql子句乱序,您应该为表名使用别名,只是使它更易于阅读。 Another thing I noticed is your fields in your WHERE clause are ambiguous. 我注意到的另一件事是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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM