左连接表错误缺少别名

时间:2015-11-20 18:10:02

标签: mysql left-join aliases

我在mysql中编写了一个查询,以获取job_input表中的所有记录以及相应的输出记录(如果可以从job_output表中获取)。它给出了以下错误:

  

查询:从job_input选择ji。*作为ji left join(选择   SUM(jo.O_Total)AS Total_Output,SUM(jo.O_XS)AS XS_Output,   SUM(jo.O_S)AS ...

     

错误代码:1248每个派生表必须有自己的别名

以下是我的查询。我做错了什么?

SELECT ji.* 
    FROM `job_input` AS ji LEFT JOIN
    (SELECT 
    SUM(jo.O_Total) AS Total_Output,
    SUM(jo.O_XS) AS XS_Output,
    SUM(jo.O_S) AS S_Output,
    SUM(jo.O_M) AS M_Output,
    SUM(jo.O_L) AS L_Output,
    SUM(jo.O_XL) AS XL_Output,
    SUM(jo.O_XXL) AS XS_Output,
    SUM(jo.O_Other) AS Other_Output FROM `job_output` AS jo GROUP BY jo.`Job_InputID`)
    ON jo.`Job_InputID`= ji.`Job_InputID`

1 个答案:

答案 0 :(得分:2)

您需要为别名添加别名吗?

SELECT ji.* 
    FROM `job_input` AS ji LEFT JOIN
    (SELECT 
    jo.`Job_InputID` AS JobID
    SUM(jo.O_Total) AS Total_Output,
    SUM(jo.O_XS) AS XS_Output,
    SUM(jo.O_S) AS S_Output,
    SUM(jo.O_M) AS M_Output,
    SUM(jo.O_L) AS L_Output,
    SUM(jo.O_XL) AS XL_Output,
    SUM(jo.O_XXL) AS XS_Output,
    SUM(jo.O_Other) AS Other_Output FROM `job_output` AS jo GROUP BY jo.`Job_InputID`) AS table2
    ON table2.JobID = ji.`Job_InputID`