sql,相同的id合并
2012-03-23 16:36
309 查看
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 临时表:
表名前使用一个#号,临时表是局部的,使用两个#号,临时表是全局的,在断开连接后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表
(
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 临时表:
表名前使用一个#号,临时表是局部的,使用两个#号,临时表是全局的,在断开连接后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表
相关文章推荐
- SQL把ID相同的记录合并成同一条记录
- SQL合并 合并id相同的数据
- sqlserver合并“id值相同”的列
- 根据某一字段值相同合并字符串 - - SQL
- sql相同value,求最大的id
- 根据某一字段值相同合并字符串 - - SQL
- SQL 合并某些字段相同的记录
- 怎么合并bug_id相同的记录(bugzilla使用的mysql)?
- SQL Server select 将类型相同的行合并
- SQL 同一张表中相同字段的内容合并为一条记录(不同字段的那一列每个记录后面加逗号)
- 相同的id 取一条记录 sql
- sql 2005 中如何自动生成不重复的,长度相同字符串作为一个数据表的id
- SQL强化:将相同的或连续的时间段合并
- 用SQL语句删除除了id不同,其他都相同的学生表信息
- SQL 合并多行记录的相同字段值
- 根据某一字段值相同合并字符串 - - SQL
- (转载)SQL 2005 当我们想将某个字段相同的几行数据合并为一列显示的时候 合并列值
- (转载)SQL 2005 当我们想将某个字段相同的几行数据合并为一列显示的时候 合并列值
- SQL 2005 当我们想将某个字段相同的几行数据合并为一列显示的时候 合并列值
- sql 多列数据相同的情况下进行合并。