我想把查询的多条记录合为一条怎么办?
时间:2011-12-12
来源:互联网
SQL code
MemberID Name Sex Email Interest 000001 张三 男 aaa@hao.com 打球 000001 张三 男 aaa@hao.com 游泳 000001 张三 男 bbb@hao.com 看电影
我想把这些记录何为一条,如:
SQL code
MemberID Name Sex Email Interest 000001 张三 男 aaa@hao.com 打球、游泳、看电影
怎么办?
作者: xp447196763 发布时间: 2011-12-12
作者: xp447196763 发布时间: 2011-12-12
if object_id('tb') is not null drop table tb go create table tb ( MemberID varchar(10), Name varchar(10), Sex varchar(10), Email varchar(20), Interest varchar(10) ) go insert into tb select '000001','张三','男','[email protected]','打球' union all select '000001','张三','男','[email protected]','游泳' union all select '000001','张三','男','[email protected]','看电影' go select MemberID,Name=Max(Name),Sex=Max(Sex),Email=Max(Email), Interest=stuff((select ','+Interest from tb where MemberID=a.MemberID for xml path('')),1,1,'') from tb a group by MemberID go /* MemberID Name Sex Email Interest ---------- ---------- ---------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 000001 张三 男 [email protected] 打球,游泳,看电影 (1 行受影响) */
作者: pengxuan 发布时间: 2011-12-12
--还是那个语句,数据变一下 if object_id('tb') is not null drop table tb go create table tb ( MemberID varchar(10), Name varchar(10), Sex varchar(10), Email varchar(20), Interest varchar(10) ) go insert into tb select '000001','张三','男','[email protected]','打球' union all select '000001','张三','男','[email protected]','游泳' union all select '000001','张三','男','[email protected]','看电影' go select MemberID,Name=Max(Name),Sex=Max(Sex),Email=Max(Email), Interest=stuff((select ','+Interest from tb where MemberID=a.MemberID for xml path('')),1,1,'') from tb a group by MemberID go /* MemberID Name Sex Email Interest ---------- ---------- ---------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 000001 张三 男 [email protected] 打球,游泳,看电影 (1 行受影响) */
作者: pengxuan 发布时间: 2011-12-12
作者: szstephenzhou 发布时间: 2011-12-12
作者: q465897859 发布时间: 2011-12-12
create table xp447196763 (MemberID varchar(8), name varchar(4), Sex varchar(4), Email varchar(15), Interest varchar(8)) insert into xp447196763 select '000001', '张三', '男', '[email protected]', '打球' union all select '000001', '张三', '男', '[email protected]', '游泳' union all select '000001', '张三', '男', '[email protected]', '看电影' declare @Interests varchar(50)='' select @Interests=@Interests+Interest+'、' from xp447196763 select MemberID,name,Sex,Email, left(@Interests,len(@Interests)-1) Interest from xp447196763 group by MemberID,name,Sex,Email MemberID name Sex Email Interest -------- ---- ---- --------------- -------------------------------------------------- 000001 张三 男 aaa@hao.com 打球、游泳、看电影 (1 row(s) affected)
作者: ap0405140 发布时间: 2011-12-12
合并列值 --******************************************************************************************* 表结构,数据如下: id value ----- ------ 1 aa 1 bb 2 aaa 2 bbb 2 ccc 需要得到结果: id values ------ ----------- 1 aa,bb 2 aaa,bbb,ccc 即:group by id, 求 value 的和(字符串相加) 1. 旧的解决方法(在sql server 2000中只能用函数解决。) --============================================================================= create table tb(id int, value varchar(10)) insert into tb values(1, 'aa') insert into tb values(1, 'bb') insert into tb values(2, 'aaa') insert into tb values(2, 'bbb') insert into tb values(2, 'ccc') go --1. 创建处理函数 CREATE FUNCTION dbo.f_strUnite(@id int) RETURNS varchar(8000) AS BEGIN DECLARE @str varchar(8000) SET @str = '' SELECT @str = @str + ',' + value FROM tb WHERE id=@id RETURN STUFF(@str, 1, 1, '') END GO -- 调用函数 SELECt id, value = dbo.f_strUnite(id) FROM tb GROUP BY id drop table tb drop function dbo.f_strUnite go /* id value ----------- ----------- 1 aa,bb 2 aaa,bbb,ccc (所影响的行数为 2 行) */ --=================================================================================== 2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。) create table tb(id int, value varchar(10)) insert into tb values(1, 'aa') insert into tb values(1, 'bb') insert into tb values(2, 'aaa') insert into tb values(2, 'bbb') insert into tb values(2, 'ccc') go -- 查询处理 SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY( SELECT [values]= STUFF(REPLACE(REPLACE( ( SELECT value FROM tb N WHERE id = A.id FOR XML AUTO ), ' <N value="', ','), '"/>', ''), 1, 1, '') )N drop table tb /* id values ----------- ----------- 1 aa,bb 2 aaa,bbb,ccc (2 行受影响) */ --SQL2005中的方法2 create table tb(id int, value varchar(10)) insert into tb values(1, 'aa') insert into tb values(1, 'bb') insert into tb values(2, 'aaa') insert into tb values(2, 'bbb') insert into tb values(2, 'ccc') go select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '') from tb group by id /* id values ----------- -------------------- 1 aa,bb 2 aaa,bbb,ccc (2 row(s) affected) */
作者: ssp2009 发布时间: 2011-12-12
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28