2011-03-14 246 views
0

請大家看我的查詢SQL子查詢返回多個值

select tms.TeamName, 
(select loc.LocID from Locations loc, Users usr where loc.UserID = usr.UserID and usr.TeamID = tms.TeamID) 
from Teams tms 

在這種情況下LOCID返回多個值。我希望它以逗號分隔值的單列形式出現。我該如何繼續?

在此先感謝 - [?在MS SQL Server 2005中模擬GROUP_CONCAT MySQL的函數]馬諾

+0

的可能重複(http://stackoverflow.com/questions/451415/simulating-group-concat- mysql-function-in-ms-sql-server-2005) – 2011-03-14 11:11:05

回答

2
select 
    tms.TeamName, 
    stuff(
    (select ','+cast(loc.LocID as varchar(10)) 
    from Locations as loc 
     inner join Users as usr 
     on loc.UserID = usr.UserID 
    where usr.TeamID = tms.TeamID 
    for xml path('')), 1, 1, '') 
from Teams as tms 
+0

謝謝Mikael Eriksson,它對我來說真的很有用,這正是我想要的。謝謝 – Manoj 2011-03-15 13:53:08