我有两个表 21相同的列名 ,但数据不同。我想加入表,但访问两个列(row [“phase1”],row [“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 :(得分:1)
您可以为第一个表中的所有列保留*
,并为table2中的列显式指定别名
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
语法。