初学SQL。问个问题

igand 2006-01-23 05:35:42
2个SQL表。能否合并??
如果能合并的话,要注意点什么??
...全文
74 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugchen888 2006-01-23
  • 打赏
  • 举报
回复
SELECT col1, col2, col3
FROM table1
UNION all
SELECT col4, col5, col6
FROM table2

要求对应栏位的数据据类型相同,如col1和col4,col2和col5,col3和col6。
-狙击手- 2006-01-23
  • 打赏
  • 举报
回复
还是横向的
???
declare @t table (a int)
insert @t select 1 union select 2 union select 3 union select 4
union select 5 union select 6 union select 7
union select 8 union select 9


select * from @t a,@t b where a.a = b.a

/*

a
-----------
1
2
3
4
5
6
7
8
9
|
^
a a
----------- -----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

*/
-狙击手- 2006-01-23
  • 打赏
  • 举报
回复
合 并?
select * from table1
union [all]
select * from table2

列顺序与类型要一致

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧