select into from 多个表
刚才看这标题党哥们的select into查询语句很长,
http://topic.csdn.net/u/20111104/12/9ed73061-a899-4098-98f3-4c1697169d43.html
很有意思,就突然奇想从多个表中选择列插入到一个新表中
结果悲剧了,
我想从两个表t1,t2中分别选两个列插入到t3中,一共生成4个字段
但这不是我想要的......我想要4列,还有我刚开始模拟众大神的sql coding有好的代码规范请指点,
aozugei...
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...
...全文
相关推荐
INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别
INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别介绍,需要的朋友可以参考下面的说明与条件
SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解(SQL数据库和Oracle数据库的区别)
我们经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了
解析MySQL中INSERT INTO SELECT的使用
本篇文章是对MySQL中INSERT INTO SELECT的使用进行了详细的分析介绍,需要的朋友参考下
SELECT INTO 和 INSERT INTO SELECT 两种表复制语句简单介绍
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少
数据库插入数据之select into from与insert into select区别详解
能第一次接触select...into...from...和insert into...select...有很多人都会误解, 从表面上看都是把相关信息查询出来,然后添加到一个表里,其实还远远没有这么简单,接下来,小猪就用最普通的表述给大家介绍一下这两者间的区别


