现,select 列1,列2 from 表2
需要将查询结果,也就是列1,列2加入到表1当中,请问如何实现呢?
9 个解决方案
#1
insert into 表1(列a,列b)
select 列1,列2 from 表2
试下这种方式插入。
#2
2楼的方法,试过后说“列名或所提供值的数目与表定义不匹配”
insert 我印象中是会把数据加到原有列的下面吧,需要做的是让表1多出2列来
insert 我印象中是会把数据加到原有列的下面吧,需要做的是让表1多出2列来
#3
你是想要改变表的结构?那你要先在表1多加2列
#4
表1, 表2有通过id关联。
select t1.列a, t1.列b, t2.列1, t2.列2
from 表1 as t1
left join
(select id, 列1, 列2 from 表2) as t2
on t1.id = t2.id
select t1.列a, t1.列b, t2.列1, t2.列2
from 表1 as t1
left join
(select id, 列1, 列2 from 表2) as t2
on t1.id = t2.id
#5
如果表1和表2沒关联字段,只是需要顺序的將表2的结果集添加到表1结果集显示出来可以这么做:
;with tb01 as
(
select
row_number() over(order by 列a) as id,
列a, 列b
from 表1
),
tb02 as
(
select
row_number() over(order by 列1) as id,
列1, 列2
from 表2
)
select t1.列a, t1.列b, t2.列1, t2.列2
from tb01 as t1
left join tb02 as t2
on t1.id = t2.id
;with tb01 as
(
select
row_number() over(order by 列a) as id,
列a, 列b
from 表1
),
tb02 as
(
select
row_number() over(order by 列1) as id,
列1, 列2
from 表2
)
select t1.列a, t1.列b, t2.列1, t2.列2
from tb01 as t1
left join tb02 as t2
on t1.id = t2.id
#6
能不能给点数据,然后给出想要的结果
#7
ALTER TABLE 表1 ADD [列1] INT ,[列2] INT --定义类型同表2一致
go
INSERT INTO 表1([列1],[列2])
SELECT [列1],[列2] FROM 表2
#8
多谢大家的回答,我本来以为可以省掉alter table 这一步的,看样子还是不行的啊
#9
不改变表结构是没办法让表多两列的。
只能在结果集里多两列结果
#1
insert into 表1(列a,列b)
select 列1,列2 from 表2
试下这种方式插入。
#2
2楼的方法,试过后说“列名或所提供值的数目与表定义不匹配”
insert 我印象中是会把数据加到原有列的下面吧,需要做的是让表1多出2列来
insert 我印象中是会把数据加到原有列的下面吧,需要做的是让表1多出2列来
#3
你是想要改变表的结构?那你要先在表1多加2列
#4
表1, 表2有通过id关联。
select t1.列a, t1.列b, t2.列1, t2.列2
from 表1 as t1
left join
(select id, 列1, 列2 from 表2) as t2
on t1.id = t2.id
select t1.列a, t1.列b, t2.列1, t2.列2
from 表1 as t1
left join
(select id, 列1, 列2 from 表2) as t2
on t1.id = t2.id
#5
如果表1和表2沒关联字段,只是需要顺序的將表2的结果集添加到表1结果集显示出来可以这么做:
;with tb01 as
(
select
row_number() over(order by 列a) as id,
列a, 列b
from 表1
),
tb02 as
(
select
row_number() over(order by 列1) as id,
列1, 列2
from 表2
)
select t1.列a, t1.列b, t2.列1, t2.列2
from tb01 as t1
left join tb02 as t2
on t1.id = t2.id
;with tb01 as
(
select
row_number() over(order by 列a) as id,
列a, 列b
from 表1
),
tb02 as
(
select
row_number() over(order by 列1) as id,
列1, 列2
from 表2
)
select t1.列a, t1.列b, t2.列1, t2.列2
from tb01 as t1
left join tb02 as t2
on t1.id = t2.id
#6
能不能给点数据,然后给出想要的结果
#7
ALTER TABLE 表1 ADD [列1] INT ,[列2] INT --定义类型同表2一致
go
INSERT INTO 表1([列1],[列2])
SELECT [列1],[列2] FROM 表2
#8
多谢大家的回答,我本来以为可以省掉alter table 这一步的,看样子还是不行的啊
#9
不改变表结构是没办法让表多两列的。
只能在结果集里多两列结果