2012-11-06 51 views
0

我有2個表,每個表有3列。我想一列,使得每個表一列中的其他在sql中添加2列到一個列表中

eg:- suppose one column in a table contains hai, how, are, you. 
and another column in another column contains i, am, fine. 
i want a query which gives hai, how, are, you,i,am,fine. in just one column 

有誰能夠給出一個查詢在SQL中......

回答

2

如果我正確理解你的模式,你有後apended一個這

Table1: Column1 
      hai, 
      how, 
      are, 
      you. 

Table2: Column2 
      i, 
      am, 
      fine. 

做到這一點:

Insert Into Table1 (Column1) 
Select Column2 From Table2 

你會得到這樣的:

Table1: Column1 
     hai, 
     how, 
     are, 
     you. 
     i, 
     am, 
     fine. 

如果你有3列 然後只是這樣做:

Insert Into Table1 (Column1, Column2, Column3)  //the (Column1, Column2, Column3) is not neccessary if those are the only columns in your Table1 
Select Column1, Column2, Column3 From Table2  //the Select Column1, Column2, Column3 could become Select * if those are the only columns of your Table2 

編輯:這樣做,如果你不想修改任何表。

Select Column1, Column2, Column3 
From Table1 
UNION ALL 
Select Column1, Column2, Column3 
From Table2 
+0

很酷,我編輯了3列的答案。 –

+0

但是你正在修改表1中的列,這不是我想要的......需要一種方法而不修改數據庫中的數據。 – stallion

+0

你只需要展示他們?與選擇語句? –

2

你的問題不是很清楚。它的一個解釋是,要UNION兩個:

select column 
from table1 
union 
select column 
from table2; 

如果你真的想從兩個表(而不是不同的值)的所有行,UNION ALL會比UNION更快。

如果您希望某些順序的行確保指定ORDER BY子句。

相關問題