社区
应用实例
帖子详情
请问怎样用SQL语句将两个表的内容并起来?
goodsong
2004-09-03 12:04:25
table1
col1 col2
1 2
2 3
table 2
col1 col2 col3
1 2 3
1 2 4
2 3 4
合并后为
col1 col2 col1 col2 col3
1 2 1 2 3
2 3 1 2 4
NULL NULL 2 3 4
即两个表的记录条数不一样多,现在要将两个表合成一张表,不足的地方补上NULL
各位帮帮忙!
...全文
159
7
打赏
收藏
请问怎样用SQL语句将两个表的内容并起来?
table1 col1 col2 1 2 2 3 table 2 col1 col2 col3 1 2 3 1 2 4 2 3 4 合并后为 col1 col2 col1 col2 col3 1 2 1 2 3 2 3 1 2 4 NULL NULL 2 3 4 即两个表的记录条数不一样多,现在要将两个表合成一张表,不足的地方补上NULL 各位帮帮忙!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
yesterday2000
2004-09-03
打赏
举报
回复
select
isnull (a.col1,null) as col1,
isnull (a.col2,null) as col2,
isnull (b.col1,null) as col1,
isnull (b.col2,null) as col2,
isnull (b.col3,null) as col3
from #tb1 a full join #tb2 b
on a.xh=b.xh
yesterday2000
2004-09-03
打赏
举报
回复
create table table1 (col1 int,col2 int )
create table table2 (col1 int,col2 int,col3 int)
insert into table1
select 1,2
union
select 2,3
insert into table2
select 1,2,3
union
select 1,2,4
union
select 2,5,8
union
select 3,6,7
select identity(int,1,1) as xh,* into #tb1 from table1
select identity(int,1,1) as xh,* into #tb2 from table2
select a.*,b.* from #tb1 a full join #tb2 b on a.xh=b.xh
goodsong
2004-09-03
打赏
举报
回复
谢谢各位老大!还有一个问题,请看一下
http://community.csdn.net/Expert/topic/3336/3336189.xml?temp=8.512515E-02
马前小卒
2004-09-03
打赏
举报
回复
up
zjcxc
2004-09-03
打赏
举报
回复
--测试
--测试数据
create table table1 (col1 int,col2 int)
insert table1 select 1,2
union all select 2,3
create table table2 (col1 int,col2 int,col3 int)
insert table2 select 1,2,3
union all select 1,2,4
union all select 2,3,4
go
--合并查询
select id=identity(int),col1,col2 into #t1 from table1
select id=identity(int),col1,col2,col3 into #t2 from table2
select a.col1,a.col2
,b.col1,b.col2,b.col3
from #t1 a full join #t2 b
on a.id=b.id
drop table #t1,#t2
go
--删除测试
drop table table1,table2
/*--测试结果
col1 col2 col1 col2 col3
----------- ----------- ----------- ----------- -----------
1 2 1 2 3
2 3 1 2 4
NULL NULL 2 3 4
(所影响的行数为 3 行)
--*/
zjcxc
2004-09-03
打赏
举报
回复
--上面写错了一点
select id=identity(int),col1,col2 into #t1 from table1
select id=identity(int),col1,col2,col3 into #t2 from table2
select a.col1,a.col2
,b.col1,b.col2,b.col3
from #t1 a full join #t2 b
on a.id=b.id
drop table #t1,#t2
zjcxc
2004-09-03
打赏
举报
回复
select id=identity(int),col1,col2 into #t1 from table1
select id=identity(int),col1,col2,col3 into #t2 from table3
select a.col1,a.col2
,b.col1,b.col2,b.col3
from #t1 a full #t2 b
on a.id=b.id
drop table #t1,#t2
如何用一条
SQL语句
删除
两个
关联
表
的记录?
有
两个
表
:group_file和teach_classroom,其中的group_file.group_id和teach_classroom.classroom_id对应唯一一条语句删除
两个
表
的
内容
:select a.*,b.classroom_namefrom group_file a,teach_classroom bwhere a....
mysql中
两个
表
连接_
SQL语句
中
两个
表
的连接
左连接 left join 或 left outer join
SQL语句
:select * from student left join score on student.Num=score.Stu_id;2.右连接 right join 或 right outer join
SQL语句
:select * from student right join score on ...
php同时删除
两个
表
中的数据,一条
sql语句
可以同时对
两个
表
做新增,更新,删除操作吗?...
1:一条
sql语句
可以同时对
两个
表
做新增,更新,删除操作吗?2:一条
sql语句
能同时对多
表
操作的只有select语句吗?3:如果将多条 新增/更新/删除 操作语句使用分号隔开执行呢?4:如果将多条select语句使用分号隔开...
使用
SQL语句
将数据库中的
两个
表
合并成一张
表
select * into 新
表
名 from (select * from T1 union all select * from T2) 这个语句可以实现将合并的数据追加到一个新
表
...
两个
表
,
表
1
表
2 如果要将
表
1的数据并入
表
2用以下语句即可 insert into
表
2(字段1,字段
SQL语句
多
表
连接查询语法
总结:内连接就是
两个
表
的交集 ,左外连接就是左边
表
加两
表
交集 ,右外连接就是右边
表
加两
表
交集 一、外连接 1.左连接 left join 或 left outer join
SQL语句
:select * from student left join score on ...
应用实例
27,579
社区成员
68,558
社区内容
发帖
与我相关
我的任务
应用实例
MS-SQL Server 应用实例
复制链接
扫一扫
分享
社区描述
MS-SQL Server 应用实例
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章