1

How to merge multiple tables with difference column name in one table?

I have table 1 and table 2, so i want to merge fields of table2 into table1.

For example:

Table 1

ID NAME
1  A
2  B
3  C

Table 2

SEX Address
M   A
F   B
M   C

Result that I need as the following:

Result

ID NAME SEX  ADDRESS
1  A    M    A
2  B    F    B
3  C    M    C

How to do that in mysql ?

4

2 回答 2

0

这是 SQLFiddel 工作演示

以下是您可以尝试的查询

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
于 2013-08-19T09:54:57.437 回答
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;
于 2013-08-19T09:10:04.983 回答