执行mysql查询时是否出现重复的列名错误?

时间:2014-04-07 06:59:35

标签: mysql sql

SELECT t1.*,t2.* FROM  

(SELECT pp.id as ppid, pp.*,tab3.*,tab2.*  FROM tab1 AS pp 
LEFT JOIN tab3 ON pp.id = tab3.name_id 
LEFT JOIN tab2 ON pp.id = tab2.name_id) AS t1

JOIN 
(
SELECT tab2.id as colname_id,tab2.*,tab3.* FROM tab2 
LEFT JOIN tab3 ON tab2.coltestconsent_id = tab3.coltestconsent_ID
) AS t2

WHERE t1.ppid = t2.colname_id;

描述:上面的查询未运行其创建错误:

错误代码:1060 列名称重复' id'

为了使上述查询成功,我必须使用列名而不是" *" 如下所示:

SELECT t1.*,t2.* FROM  

(SELECT pp.first_name,pp.id ppid,tab3.id AS coltestrisk_id,tab2.id AS coltest_id  FROM tab1 AS pp 
LEFT JOIN tab3 ON pp.id = tab3.name_id 
LEFT JOIN tab2 ON pp.id = tab2.name_id) AS t1

JOIN 
(
SELECT tab2.coltestconsent_id AS coltestconsent_id,tab3.coltestconsent_ID AS colriskconsent_id,tab2.name_id AS colname_id,tab3.name_id AS coltest_nameid FROM tab2 
LEFT JOIN tab3 ON tab2.coltestconsent_id = tab3.coltestconsent_ID
) AS t2

WHERE t1.ppid = t2.colname_id;

要求:我想获取所有表的所有列值。每个表有60-80列。我不想手动将列名放在我的查询中,因为它们很多列。

2 个答案:

答案 0 :(得分:0)

不需要派生表:

SELECT t1_pp.id AS ppid, t2_tab2.id AS colname_id, *
FROM   (
              tab1 AS t1_pp 
    LEFT JOIN tab3 AS t1_tab3 ON t1_pp.id = t1_tab3.name_id 
    LEFT JOIN tab2 AS t1_tab2 ON t1_pp.id = t1_tab2.name_id
       ) JOIN (
              tab2 AS t2_tab2
    LEFT JOIN tab3 ON t2_tab3 USING (coltestconsent_id)
       ) ON t1_pp.id = t2_tab2.id

答案 1 :(得分:0)

你没有提到*你想要的代码:

可能是因为*中的select *选择了two columns name同名。

SELECT pp.id as ppid, pp.*,tab3.*,tab2.*  FROM tab1 AS pp