sql语句 怎么从一张表中查询数据插入到另一张表中

sql语句 怎么从一张表中查询数据插入到另一张表中,第1张

sql语句从一张表中查询数据插入到另一张表中的方法如下:

1、select * into destTbl from srcTbl。

2、insert into destTbl(fld1, fld2) select fld1, 5 from srcTbl。

以上两句都是将 srcTbl 的数据插入到 destTbl,但两句又有区别的:

第一句(select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建。

第二句(insert into select from)要求目标表(destTbl)存在卜凯漏,由于目标表已经存在,所以我们除了插入源表(srcTbl)的字段外,还可以插入常量。

拓展资料:

结构化查询语言(Structured Query Language)简称SQL,结孙逗构化查询语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。sql 语句就是对数据库进行 *** 作的一种语言。

常见语句:

1、更新:update table1 set field1=value1 where 范围。

2、查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)。

3、排序:select * from table1 order by field1,field2 [desc]。

4、求和:select sum(field1) as sumvalue from table1。

5、平均:select avg(field1) as avgvalue from table1。

6、最大:select max(field1) as maxvalue from table1。

7、最小:select min(field1) as minvalue from table1[searator]。型烂

你insert into test3插入了三项,但是后面select的结果只有1项梁乱。。

要统一即可。

不知道你是不要的这个结果:

declare @i varchar(50)set @i='75'

while @i<'80'

begin

insert into test3(MP,ZS,CHD)

values(select @i,

select count(序号孝纯)from text where 住址 like '%XX路'+@i+'%',select count(序号) from text where 住橡慎档址 like '%XX路'+@i+'%' and 出生日期>'19980101')

set @i=@i+2

end

假设

要查询的表:

表名T1,

字段:A0,A1,A2,A3

要插入的表:

表名T2,

字段:B0,B1,B2,B3

实现方法1

如果T1表和T2表结构一致可以这样做:

INSERT INTO T2 SELECT * FROM T1 WHERE 查询条件

按照查询出来的顺序:

将T1表的A0插入到T2表的B0中,A1插入到B1中,A2插入到B2中,A3插入到B3中

实现方法2:

如果T1表和T2表结构不一直,或者只把T1的数据插入到T2的部分字段中去,可以这样做:

INSERT INTO T2 (B0,B1,B2) SELECT A1,A2,A3 FROM T1 WHERE 查询条件

这样就把从表T1中查询到的A1插入到T2表的B0字段,A2插入B1中,A3插入B2中,这条记录的B3则是空肢指陆。

以上方历顷法适合多条记录的查询和插入。

你可以在百度搜索里搜索:insert-select 或者是select insert 有很多这样的逗绝答案。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11991430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存