select into from 多个表

苦苦的潜行者 2011-11-04 04:50:13
刚才看这标题党哥们的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...
...全文
1594 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-11-05
  • 打赏
  • 举报
回复
create table t1([id] int,num int)
create table t2([id] int,num int)
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
;with c1 as(
select id as id1,row_number()over(order by (select 1))rn from t1 where id>3
),c2 as(
select num as num1,row_number()over(order by (select 1))rn from t1 where num<7
),c3 as(
select id as id2,row_number()over(order by (select 1))rn from t2 where id<5
),c4 as(
select num as num2,row_number()over(order by (select 1))rn from t2 where num>5
),c5 as(
select rn from c1 union select rn from c2 union select rn from c3 union select rn from c4
)select b.id1,c.num1,d.id2,e.num2
from c5 a left join c1 b on a.rn=b.rn
left join c2 c on a.rn=c.rn
left join c3 d on a.rn=d.rn
left join c4 e on a.rn=e.rn
/*
id1 num1 id2 num2
----------- ----------- ----------- -----------
6 4 1 7
NULL 6 2 9
NULL NULL 4 NULL
NULL NULL 2 NULL

(4 行受影响)

*/
go
drop table t1,t2
苦苦的潜行者 2011-11-05
  • 打赏
  • 举报
回复
众神,我想要的结果如14楼的分析,请帮忙.
-晴天 2011-11-05
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 lastone_key 的回复:]
我的方法比较笨就是把这两个表拆成4个表(每个表都有个自增ID号 )
然后把4个表联系在一起
[/Quote]

啊!
原来就有ID啊,我还用那么多的公用表达式创建序号!
稻草_木偶 2011-11-05
  • 打赏
  • 举报
回复
我的方法比较笨就是把这两个表拆成4个表(每个表都有个自增ID号 )
然后把4个表联系在一起
苦苦的潜行者 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 qianjin036a 的回复:]

要四列?
SQL code
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 sel……
[/Quote]



哥.你是我哥.
苦苦的潜行者 2011-11-04
  • 打赏
  • 举报
回复
唉,整天蹲在屋里不说话我表达能力都下降了.
我再重新整理一下.希望你们能看懂.

select [id] as id1 from t1 where [id]>3
id1
--
6
select num as num1 from t1 where num<7
num1
---
4
6
select [id] as id2 from t2 where [id]<5
id2
--
1
2
4
2
select num as num2 from t2 where num>5
num2
----
7
9

/*
最后的结果t3,应为
id1 num1 id2 num2
-- ---- --- ----
6 4 1 7
null 6 2 9
null null 4 null
null null 2 null

*/


-晴天 2011-11-04
  • 打赏
  • 举报
回复
要四列?
    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 a.id,a.num,b.id as id1,b.num as num1
from t1 a ,t2 b
/*
id num id1 num1
----------- ----------- ----------- -----------
2 4 1 3
3 6 1 3
6 9 1 3
3 8 1 3
2 4 2 5
3 6 2 5
6 9 2 5
3 8 2 5
2 4 4 7
3 6 4 7
6 9 4 7
3 8 4 7
2 4 2 9
3 6 2 9
6 9 2 9
3 8 2 9

(16 行受影响)

*/
go
drop table t1,t2
TimZhuFaith 2011-11-04
  • 打赏
  • 举报
回复
select *
into t3


from
(
select id1, num1, id2, num2 from (
select [id] as id1,num as num1 from t1 where [id]>3 or num<7
) t11,
(
select [id] as id2,num as num2 from t2 where [id]<5 or num>5
) t12) as a
苦苦的潜行者 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ssp2009 的回复:]

SQL code
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]……
[/Quote]

其实我的写法应该是错误的,我想要4列东西,
如果说想要两列的话就如筛选id和num我就不费周折问众大神了,嘻嘻.
不好意思,我实在是没事折磨自己,不小心又折磨到你们宝贵的时间了.
--小F-- 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xiaolinyouni 的回复:]
引用 4 楼 fredrickhu 的回复:

SQL code
select
a.id,a.num,b.id,b.num
from
(
select px=row_number()over(order by getdate()),[id] as id1,num as num1 from t1 where [id]>3 and num<7)a
join
(
select px=……
[/Quote]

汗 2000换identity
火才松 2011-11-04
  • 打赏
  • 举报
回复
+1,用union all是拼接行,要用join才能拼接列。用full join就可以了。
[Quote=引用 3 楼 ssp2009 的回复:]

SQL code
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]……
[/Quote]
苦苦的潜行者 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 roy_88 的回复:]

-- 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

……
[/Quote]

我知道我错了,我写到这我就虚了.所以才换了写法.
R神别见怪啊.
苦苦的潜行者 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fredrickhu 的回复:]

SQL code
select
a.id,a.num,b.id,b.num
from
(
select px=row_number()over(order by getdate()),[id] as id1,num as num1 from t1 where [id]>3 and num<7)a
join
(
select px=row_number()over(order by get……
[/Quote]

'row_number' 不是可以识别的 函数名。


我的本意说的有点含糊
我的想法是
-- (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
每一条子查询都会出来一列,分别是id1,num1,id2,num2,然后作为t3的字段生成并导入数据



gw6328 2011-11-04
  • 打赏
  • 举报
回复
是能插入的。
--小F-- 2011-11-04
  • 打赏
  • 举报
回复
select
a.id,a.num,b.id,b.num
from
(
select px=row_number()over(order by getdate()),[id] as id1,num as num1 from t1 where [id]>3 and num<7)a
join
(
select px=row_number()over(order by getdate()),[id] as id2,num as num2 from t2 where [id]<5 and num>5
)b
on
a.px=b.px
快溜 2011-11-04
  • 打赏
  • 举报
回复
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 * from
(select [id] as id1,num as num1 from t1 where [id]>3 and num<7) a
full join
(select [id] as id2,num as num2 from t2 where [id]<5 and num>5) b
on a.id1=b.id2
) as a
go
select * from t3

/*
id1 num1 id2 num2
----------- ----------- ----------- -----------
NULL NULL 4 7
NULL NULL 2 9

(2 行受影响)
中国风 2011-11-04
  • 打赏
  • 举报
回复
-- 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

不能這樣用啊,用表連接
--小F-- 2011-11-04
  • 打赏
  • 举报
回复
什么4列?

加个自增列 然后join起来。
内容概要:本文系统研究了开关频率大于谐振频率(fs>fr)工况下,移相混合控制LLC谐振变换器在低压增益区域的工作特性,深入分析其在变频与移相结合控制模式下的调制机理、工作模态划分及损耗分布规律。通过Simulink平台构建高保真仿真模型,对变换器在不同负载和输入条件下的电压增益、转换效率、关键器件电压电流应力等性能指标进行了全面仿真验证,重点探讨了其在低增益区间的软开关实现能力与效率优化潜力,旨在提升LLC变换器在宽范围输入输出应用中的动态响应与能源转换效率。; 适合人群:从事电力电子变换器设计、高频电源开发及相关领域的高校研究生、科研院所研究人员及企业研发工程师,要求具备扎实的电路理论基础、电力电子技术知识以及一定的Simulink仿真能力。; 使用场景及目标:①深入理解LLC谐振变换器在fs>fr条件下采用移相混合控制的内在工作机理与模态转换过程;②掌握利用Simulink搭建复杂谐振变换器精确仿真模型的方法与技巧;③分析并优化低压增益区的增益特性与损耗构成,为设计高效率、高功率密度的软开关电源提供理论依据和数据支持; 阅读建议:建议读者结合文中所述仿真模型,亲自复现仿真过程,重点观察不同控制参数(如移相比、开关频率)对电压增益曲线和关键波形的影响,并对比传统变频控制策略,深入探究混合控制在拓宽调压范围、提升轻载效率方面的优势,从而深化对现代高效谐振电源设计的理解。
内容概要:本文提出了一种基于粒子群优化算法(PSO)的配电网光伏储能双层优化配置模型,以IEEE33节点系统为标准算例,实现光伏发电单元与储能系统的协同选址与定容优化。该模型采用双层架构设计,上层以投资成本、运行经济性及网络损耗最小为目标优化设备配置方案,下层通过潮流计算评估系统在不同负荷场景下的运行性能,综合考虑电压稳定性、供电可靠性及可再生能源消纳能力,最终通过Matlab编程实现完整求解流程,为高渗透率分布式电源接入背景下的配电网规划提供了有效的技术支撑。; 适合人群:具备电力系统分析基础和Matlab编程能力的研究生、高校科研人员及从事新能源并网、智能配电网规划与优化的工程技术人员。; 使用场景及目标:①研究含高比例光伏接入的配电网规划与运行协同优化问题;②掌握双层优化建模方法与粒子群算法在复杂电力系统问题中的应用技巧;③为实际工程中分布式光伏与储能系统的科学选址与容量配置提供理论依据与仿真验证平台。; 阅读建议:建议读者结合Matlab代码深入理解双层迭代求解机制,重点关注算法收敛性分析、参数敏感性测试,并可通过更换初始种群、调整权重因子或引入其他标准测试系统(如IEEE69节点)进行对比实验,进一步验证所提模型的普适性与鲁棒性。
内容概要:本文围绕双机并联虚拟同步发电机(VSG)在微电网中的功率分配、黑启动、虚拟阻抗与预同步控制展开,基于Simulink平台构建了完整的微电网系统仿真模型。重点研究了VSG在双机并联运行下的有功/无功功率均分控制策略,通过引入虚拟阻抗技术有效解决了因线路阻感比差异导致的功率分配不均问题。同时,设计了微电网黑启动流程与并网预同步控制模块,实现了待并网系统与主网在电压幅值、频率和相位上的精确同步,显著降低了并网冲击电流。系统整合了VSG控制、下垂控制、虚拟阻抗、锁相环(PLL)及预同步逻辑等关键环节,全面验证了多VSG协同运行的稳定性、自主恢复能力与并网可靠性。; 适合人群:具备电力系统、电力电子及自动控制等相关专业知识,从事微电网、分布式发电、VSG控制与并网技术研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①深入理解双机并联VSG系统中功率分配不均的机理及虚拟阻抗的补偿作用;②掌握微电网黑启动全过程及预同步控制的关键技术要点;③学习并实践基于Simulink的微电网多层次、多目标控制策略的建模与仿真方法;④为相关科研课题、毕业设计或实际工程项目提供可复现、可拓展的技术方案与仿真参考。; 阅读建议:建议结合提供的Simulink模型文件进行同步学习,重点关注VSG控制参数整定、虚拟阻抗设计原则、预同步切换逻辑等核心模块的实现细节,并可通过改变负载投切、线路参数或初始频率偏差等条件进行多工况仿真测试,以深入探究系统的动态响应特性与鲁棒性。

34,875

社区成员

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

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