【sql相同的id合并】 r语言如何把id相同的字段合并

create table deom

(

id int primary key identity(1,1),

code varchar(32) ,

txt varchar(64)

)

insert into deom(code,txt)

select "102","科学" union

select "102","数学" union

select "102","数学1" union

select "101","美术" union

select "101","体育" union

select "101","体育1"

create function s_str(@code varchar(20))

returns varchar(100)

begin

declare @a varchar(1000)

set @a=""

select @a=@a+","+txt from deom where code=@code

set @a=stuff(@a,1,1,"")

return @a

end

go

select code,name=dbo.s_str(code) from deom group by code

效果:

sql相同的id合并1

表名前使用一个#号,临时表是局部的,使用两个#号,临时表是全局的,在断开连接后sql会自动删除临时表

create table #a

(

id int,

name varchar(50)

)

insert into #a(id,name) values(1,"123")

select * from #a

drop table #a

临时表除了名称前多了#号外,其他操作与普通表完全一样。

tb_Student是已建立好的表,我们通过临时表temp把tb_Student表中的内容复制到tb_lizi表中,可以使用如下的代码实现:

use mcf

SELECT * INTO #temp FROM tb_Student

SELECT * INTO tb_lizi FROM #temp

执行后断开sql连接并重新连接(也可以退出sq再l重新启动sql),发现tb_lizi表中的内容tb_Student表中的内容完全一致,实现了复制,同时我们没有用代码删除temp表,但mcf数据库中却没有temp表了,这是因为断开连接时sql自动删除了temp表