SQL group BY 合併字段用逗號隔開

1.關聯多表後 根據某個字段做爲分組條件,其餘合併到新列中,效果以下圖spa

-------->code

 

代碼:xml

select KUserID, KOrderID=stuff((select ','+Convert(varchar(10),KOrderID) from VOrder2 t where KUserID=tb.KUserID for xml path('')), 1, 1, '') from VOrder2 tb group by KUserID

 

2.單個表直接轉換單列合併blog

select stuff((select ','+Convert(varchar(10),PID)  from AA  where State=1 for xml path('')), 1, 1, '') as PID
相關文章
相關標籤/搜索