Skip to content
Advertisement

Duplicate column name – inner join

This is my query :

SELECT FinancialMoein.*,
       FinancialMoein.kol_code as kol_code ,
       TBFC.kol_code as parent_code 
FROM (
       SELECT TP.*,
              ifnull(TP.kol_code,TP.moein_code) as code,
              ifnull(TR.title, TP.title_en) AS title,
              ifnull(TR.description, TP.description_en) AS description
       FROM Tb_Financial_Coddings TP LEFT JOIN 
             (
               SELECT title,
                      description,
                      financial_codding_id 
               FROM Tb_Financial_Coddings_Translations 
               WHERE locale = @Locale
             ) TR  
       ON TR.financial_codding_id = TP.id
       WHERE TP.type = 'kol' AND TP.deleted_at IS NULL
     ) as FinancialMoein INNER JOIN Tb_Financial_Coddings TBFC 
ON FinancialMoein.parent_id = TBFC.id;

but I got this error message:

Duplicate column name ‘kol_code’

Advertisement

Answer

FinancialMoein.* in select clause already includes the column kol_code since it is derived using table Tb_Financial_Coddings which has the column kol_code. Then again you are selecting it with the same name, hence there is issue. You have to keep the different names for all the columns. You may try below query to ignore the issue –

SELECT FinancialMoein.*,
       FinancialMoein.kol_code as kol_code_2,
       TBFC.kol_code as parent_code 
FROM (
       SELECT TP.*,
              ifnull(TP.kol_code,TP.moein_code) as code,
              ifnull(TR.title, TP.title_en) AS title,
              ifnull(TR.description, TP.description_en) AS description
       FROM Tb_Financial_Coddings TP LEFT JOIN 
             (
               SELECT title,
                      description,
                      financial_codding_id 
               FROM Tb_Financial_Coddings_Translations 
               WHERE locale = @Locale
             ) TR  
       ON TR.financial_codding_id = TP.id
       WHERE TP.type = 'kol' AND TP.deleted_at IS NULL
     ) as FinancialMoein INNER JOIN Tb_Financial_Coddings TBFC 
ON FinancialMoein.parent_id = TBFC.id;

Rather you may just remove the column FinancialMoein.kol_code as kol_code, from the outer select to ignore the error.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement