2013-05-18 43 views
0

我有两个表21相同的列名,但数据不同。我想加入表格,但访问列(行[“phase1”],行[“other_phase1”]):我如何在select语句中用下面的小语句重新命名/别名:如何在选择时重命名表中的所有列?

SELECT t_process.*,t_task_process.* AS other_column WHERE ... 

我是否应该为所有21列编写AS语句或者有简明的方法? 如果不是没有办法得到结果作为一个数组与这些键:t_process.phase1,....,t_task_process.phase1,...?
另外我不能在数据库中重命名它们。
疗法原来的查询是:

SELECT t_process.*,t_task_process.* 
FROM t_process,t_task_process 
WHERE t_process.id = t_task_process.id 
AND (t_process.phase1<>t_task_process.phase1 
OR t_process.phase2<>t_task_process.phase2 
OR t_process.phase3<>t_task_process.phase3 
OR t_process.phase4<>t_task_process.phase4 
OR t_process.phase5<>t_task_process.phase5 
OR t_process.phase6<>t_task_process.phase6 
OR t_process.phase7<>t_task_process.phase7 
OR t_process.phase8<>t_task_process.phase8 
OR t_process.phase9<>t_task_process.phase9 
OR t_process.phase19<>t_task_process.phase10 
OR t_process.phase11<>t_task_process.phase11 
OR t_process.phase12<>t_task_process.phase12 
OR t_process.phase13<>t_task_process.phase13 
OR t_process.phase14<>t_task_process.phase14 
OR t_process.phase15<>t_task_process.phase15 
OR t_process.phase16<>t_task_process.phase16 
OR t_process.phase17<>t_task_process.phase17 
OR t_process.phase18<>t_task_process.phase18 
OR t_process.phase19<>t_task_process.phase19 
OR t_process.phase20<>t_task_process.phase20  
OR t_process.phase21<>t_task_process.phase21) 

没有什么力量让MySQL能够有我想要的。我只想知道如果有一个简洁的方式或不。

+0

我通常只是将模式剪切并粘贴到记事本中,然后剪切并粘贴t1。和t2。等在他们面前,然后剪切和粘贴整个块,我当然不会输入它。你会垂直向下,所以第一波的粘贴量为t1。在剪贴板 – Drew

+0

请发布您的总查询。不可理解。 –

+0

您可以编写一个使用'INFORMATION_SCHEMA'生成动态查询的存储过程。 – Barmar

回答

1

您可以留下*在第一个表中的所有列在表2

SELECT t1.*, 
     t2.phase1 phase1_2, 
     t2.phase2 phase2_2, 
     t2.phase3 phase3_2, 
     ... 
    FROM t_process t1 JOIN t_task_process t2 
    ON t1.phase1 <> t2.phase1 
    OR t1.phase2 <> t2.phase2 
    OR t1.phase3 <> t2.phase3 
    ... 

SQLFiddle

在一个侧面说明显式地指定列的别名:使用标准JOIN语法。

+0

没关系,你确定没有简明的方法? – HPM

+0

@HPM不是我知道的。但这样你只需要明确指定其中的一半。 – peterm

+0

某些数据库产品在发现重复项时会自动重新命名列,但这种行为并非标准行为,因此既不是在任何地方都实施,也不是使用常规语法构建的。 – DougM

相关问题