select a.* from YG_BRSYK a left join(SELECT DISTINCT SYXH, STUFF((SELECT '、'+MS FROM #lsb where SYXH=t.SYXH FOR XML PATH('')),1,1,'') AS MS FROM #lsb as t) c on a.SYXH=c.SYXH WHERE c.MS IS NOT NULL order by RYBQ
--注:left join...on 爲左關聯,保留左邊全部的數據,右表沒有的數據爲NULL值。加where條件控制表的輸出C.MS不爲NULL。所以查詢的個數與c表一致760個
select a.* from YG_BRSYK a --801331個
SELECT DISTINCT SYXH,
STUFF((SELECT '、'+MS FROM #lsb where SYXH=t.SYXH FOR XML PATH('')),1,1,'') AS MS
FROM #lsb as t 670個
https://www.cnblogs.com/lcs-java/articles/8465605.htmlhtml