将2列附加到sql中的一个列表中

时间:2012-11-06 09:42:26

标签: sql database

我有2个表,每个表有3列。我想得到一个列,使得每个表中的一列被一个接一个地附加

 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 个答案:

答案 0 :(得分:2)

如果我正确理解您的架构,那么就有了这个

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

答案 1 :(得分:2)

你的问题不是很清楚。对它的一种解释是你想要UNION这两个:

select column
from table1
union
select column
from table2;

如果你真的想要来自两个表的所有行(而不是不同的值),UNION ALL将比UNION更快。

如果您希望按特定顺序排列,请务必指定ORDER BY子句。