sql合并多行记录

时间:2021-12-19 02:34:43

MySQL

 使用关键字 group_concat,具体示例如下:
 select stuID, group_concat(borrowId) as dd from t_borrow

SqLServer

            select 
            companyIds = (
                stuff(
                    (select ',' + company_id from FND_USER_COMPANY where user_id = A.user_id for xml path('')),
                    1,
                    1,
                    ''
                )
            ) 
        from FND_USER_COMPANY as A 
        WHERE USER_ID=1
        group by user_id;

以上示例是查询用户所在公司的所有ID