[SQL]把同一字段里的多行数据用一行显示


declare @t table(id int,num int)
insert @t
select 1,2 union all
select 2,4 union all
select 3,6
--select * from @t

----查询
declare @idList varchar(1000),@numList varchar(1000)
set @idList = ''
set @numList = ''
select 
@idList = case @idList when '' then '' else @idList + ',' end + rtrim(id),
@numList = case @numList when '' then '' else @numList + ',' end + rtrim(num)
from @t

select @idList as id,@numList as num

/*结果
id      num
------------------
1,2,3   2,4,6
*/

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM