select into from 多个表

苦苦的潜行者 苦苦的潜行者 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
刚才看这标题党哥们的select into查询语句很长,
http://topic.csdn.net/u/20111104/12/9ed73061-a899-4098-98f3-4c1697169d43.html
很有意思,就突然奇想从多个表中选择列插入到一个新表中
结果悲剧了,
我想从两个表t1,t2中分别选两个列插入到t3中,一共生成4个字段
/*
为了学习sql,强制自己用sql操作表,即使再简单的查询也要用sql测试
作者:Jully
*/

if object_id('t1') is not null
drop table t1
go
if object_id('t2') is not null
drop table t2
go
create table t1
(
[id] int,
num int
)
go
create table t2
(
[id] int,
num int
)
go
insert t1 select 2,4
union all select 3,6
union all select 6,9
union all select 3,8
go
insert t2 select 1,3
union all select 2,5
union all select 4,7
union all select 2,9
go
if object_id('t3') is not null
drop table t3
go
-- select
-- (select [id] from t1 where [id]>3) as id1,
-- (select num from t1 where num<7) as num1,
-- (select [id] from t2 where [id]<5) as id2,
-- (select num from t2 where num>5) as num2
-- into t3 //这是一开始的写法,t3表能生成,且有4个字段 id1,num1,id2,num2 但是语句不成功
--改为如下
select *
into t3
from
(
select [id] as id1,num as num1 from t1 where [id]>3 and num<7
union all
select [id] as id2,num as num2 from t2 where [id]<5 and num>5
) as a
go
select * from t3

/*
但是结果为

(所影响的行数为 2 行)

id1 num1
--- ----
4 7
2 9
-------
*/

但这不是我想要的......我想要4列,还有我刚开始模拟众大神的sql coding有好的代码规范请指点,
aozugei...
...全文

相关推荐

评论 19
收藏 已收藏
分享
返回顶部
苦苦的潜行者
MS-SQL Server 34,876 成员 254,635 内容