**嗨, 我有3张桌子:
users
certification
project
我需要为ech用户选择认证列表和项目列表,如:
--------------------------------------------------------------------
userid | fullname | list of certification | list of project
--------------------------------------------------------------------
1 | xX | ITIL ,Cisco,.. | PROj 1 ,Proj 2 ...
我试试这个**
select users.fullname,users.team,users.jobtitle,certif= STUFF(
(SELECT ','+ certifiacte
FROM dbo.certification a
INNER JOIN dbo.Users ar ON ar.UserId = a.UserId
FOR XML PATH('')), 1, 1, ''),porj= STUFF(
(SELECT ','+ project
FROM dbo.project a
INNER JOIN dbo.Users ar ON ar.UserId = a.UserId
FOR XML PATH('')), 1, 1, '')
from Users
但问题是我获得了所有行中保存在数据库中的所有认证和项目的列表。但我需要每个用户的项目和认证列表只有他
答案 0 :(得分:0)
任何时候发生这种情况,都意味着你没有相关性。您的子选择与外部查询无关。
你需要这样做:
select ar.fullname,ar.team,ar.jobtitle,certif= STUFF(
(SELECT ','+ certifiacte
FROM dbo.certification a
WHERE ar.UserId = a.UserId
FOR XML PATH('')), 1, 1, ''),porj= STUFF(
(SELECT ','+ project
FROM dbo.project a
WHERE ar.UserId = a.UserId
FOR XML PATH('')), 1, 1, '')
from Users ar