怎样用SQL语句合并两个表中的两个列

比如有 table1,table2。 table1中的names列和table2中的names列, 合并名为typ
select a.names, b.names as typ from table1 as a ,table2 as b
我用上边这个语句实现不了。谁能告诉我怎么写才行啊

第1个回答  2013-10-23
你给个条件好让两条合并成一条啊。如
select a.names, b.names as typ from table1 as a ,table2 as b where a.id=b.id追问

我是想让他们合并成一个列,不是说显示b和a id相等的列

第2个回答  2013-10-23
select * from (
select names from table1

union all
select names from table2
)a

这样就是得到2个表的数据在同一列出现追问

恩,谢谢了,不过我有点不懂后边为什么要加个a。 是语法规定吗?

追答

a 是别名,可以是任何字符,不添加会有语法错误

第3个回答  2018-03-30

建议直接用union all程序:

select names as typ

from table1

union all

select names

from table2

本回答被网友采纳
第4个回答  2013-10-23
不知道你具体什么数据库,且表述不清楚,大概写下,,,
sqlserver可以

select (a.names+ b.names) as typ
from table1 as a ,table2 as b

where a.关联字段=b.关联字段追问

是sql server的数据库, 我想把那两列列出来,不是说吧两列的内容加起来显示在一起

追答

,,那你直接用union all就可以了
select names as typ

from table1
union all
select names
from table2

本回答被提问者采纳
相似回答