SELECT oi.*, stuff((select ','+convert(varchar(25), GuestName) FROM guest g WHERE g.ordersn = oi.OrderSN AND oi.ItemId = g.ItemId FOR XML PATH('')), 1, 1, '') as CustomerName FROM orderitem oi
以上涉及的两个表是OrderItem和Guest,以属性OrderSN和ItemId连接,查询结果格式为:
用户0,用户1,用户2
代码来自:http://www.2cto.com/database/201301/182442.html