0

我有两个表,有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拥有我想要的东西。我只想知道是否有简洁的方法。

4

1 回答 1

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语法。

于 2013-05-18T04:07:26.673 回答