如何在一个表中合并具有差异列名的多个表?
我有表1和表2,所以我想将table2的字段合并到table1中。
例如:
表1
ID NAME
1 A
2 B
3 C
表2
SEX Address
M A
F B
M C
我需要的结果如下:
结果
ID NAME SEX ADDRESS
1 A M A
2 B F B
3 C M C
如何在mysql中执行此操作?
答案 0 :(得分:0)
做这样的事情:
SELECT t1.ID, t1.NAME, t2.SEX, t2.ADDRESS from table1 t1 cross join table2 t2;
或强>
SELECT t1.ID, t1.NAME, t2.SEX, t2.ADDRESS from table1 t1, table2 t2;
答案 1 :(得分:0)
<强> Here is the SQLFiddel working Demo 强>
以下是您可以尝试的查询
select T1.ID,T1.Name,T2.Sex,T2.Address
from
(select *,@Row1 := @Row1 + 1 as rownum
from Table1
join (select @Row1 := 0) r) as T1
join
(select *,@Row2 := @Row2 + 1 as rownum
from Table2
join (select @Row2 := 0) r) as T2
Where T1.rownum = T2.rownum