sqlserver模仿mysql函數FIND_IN_SET,group_concat的功能
阿新 • • 發佈:2017-05-25
group_concat find_in_set sqlserver模仿mysql
一、判斷某個逗號隔開的字段中有某個值
FIND_IN_SET(‘a‘,‘a,b,c,d‘) 用 CHARINDEX(‘,‘+‘b‘+‘,‘ , ‘,‘+‘a,b,c‘+‘,‘) > 0 替代
二、模仿mysql的group_concat的示例
SELECT id, val=( SELECT [value] +‘,‘ FROM tb AS b WHERE b.id = a.id FOR XML PATH(‘‘) ) FROM tb AS a GROUP BY id
SELECT id, [val]=STUFF( (SELECT ‘,‘+[value] FROM tb AS b WHERE b.id = a.id FOR XML PATH(‘‘)) , 1 , 1 , ‘‘ ) FROM tb AS a GROUP BY id
--刪除、創建臨時表 drop table #table1; select * into #table1 from ( SELECT myId,‘auto‘ as type )allData --把數據按myId分類鏈接,然後按鏈接後數據的長度倒序排序 select * from ( select myId, val=STUFF( (SELECT ‘、‘+type FROM #table1 AS b WHERE b.myId = ta.myId FOR XML PATH(‘‘)) , 1 , 1 , ‘‘ ) from #table1 ta group by myId ) dd order by LEN(val) desc
本文出自 “風之痕_雪虎” 博客,請務必保留此出處http://snowtiger.blog.51cto.com/12931578/1929498
sqlserver模仿mysql函數FIND_IN_SET,group_concat的功能