0

所以我有两个用户,一个导师和一个学员。

我想根据学员的技能向学员展示一份导师名单。

所以注册将是一堆复选框,上面写着......

受指导者看到:

我需要帮助

[] Branding
[] Marketing
[] Legal stuff

导师看到:

我的专长

[] Branding
[] Marketing
[] Legal stuff

是否有一个 SQL 服务器查询可以根据最常见的情况来匹配这些人?

希望这一切都有意义:\

干杯

4

2 回答 2

2

如何从这样的事情开始:

declare @Users as Table ( UserId Int Identity, UserName VarChar(10), Mentor Bit )
declare @Skills as Table (SkillId Int Identity, Skill VarChar(10) )
declare @UserSkills as Table ( UserId Int, SkillId Int )

insert into @Users ( UserName, Mentor ) values ( 'Einstein', 1 ), ( 'Dilbert', 0 ), ( 'Marie', 1 ), ( 'The Fonz', 1 )
insert into @Skills ( Skill ) values ( 'Arithmetic' ), ( 'Chemistry' ), ( 'Dancing' )
insert into @UserSkills ( UserId, SkillId ) values
  ( 1, 1 ), ( 1, 3 ),
  ( 2, 1 ), ( 2, 3 ),
  ( 3, 1 ), ( 3, 2 ), ( 3, 3 ),
  ( 4, 3 )

-- All users.
select U.*, S.*
  from @Users as U inner join
    @UserSkills as US on US.UserId = U.UserId inner join
    @Skills as S on S.SkillId = US.SkillId
  order by U.Mentor, U.UserName, S.Skill

-- Matches for user 2.
--   Should validate that they are not a mentor.
declare @StudentId as Int = 2
select UM.*, S.*,
  ( select count(42) from @UserSkills as USM inner join
    @UserSkills as USS on USS.SkillId = USM.SkillId and USS.UserId = @StudentId and USM.UserId = UM.UserId ) as 'MatchCount'
  from @Users as UM inner join
    @UserSkills as USM on USM.UserId = UM.UserId and UM.Mentor = 1 inner join
    @Skills as S on S.SkillId = USM.SkillId inner join
    @UserSkills as USS on USS.SkillId = USM.SkillId and USS.UserId = @StudentId
  order by ( select count(42) from @UserSkills as USM inner join
    @UserSkills as USS on USS.SkillId = USM.SkillId and USS.UserId = @StudentId and USM.UserId = UM.UserId ) desc,
    UM.UserName, S.Skill
于 2012-04-05T13:26:00.243 回答
0

这是一个解决方案。注意@UserSkill 表中的 is_requesting 位。单个用户可以成为一项技能的指导者和另一项技能的指导者。似乎是最合理的做法。

您可以通过再次加入表并使用一些有趣的字符串连接来扩展查询以包括每个关系的技能。

DECLARE @User TABLE
(
    id INT IDENTITY (1,1),
    name VARCHAR(100)
)

DECLARE @Skill TABLE
(
    id INT IDENTITY(1,1),
    name VARCHAR(100)
)

DECLARE @UserSkill TABLE
(
    [user_id] INT,
    skill_id INT,
    is_requesting BIT -- true if mentee asking for help, false if mentor offering help
)

INSERT INTO @User 
SELECT 'Alice' 
UNION SELECT 'Bob' 
UNION SELECT 'Charlie' 
UNION SELECT 'Doug'

SELECT * FROM @User

INSERT INTO @Skill
SELECT 'Branding' 
UNION SELECT 'Marketing' 
UNION SELECT 'Legal'

SELECT * FROM @Skill

INSERT INTO @UserSkill
SELECT 1, 1, 1
UNION SELECT 1, 2, 1
UNION SELECT 2, 2, 1
UNION SELECT 2, 3, 0
UNION SELECT 3, 1, 0
UNION SELECT 3, 2, 0
UNION SELECT 4, 2, 0
UNION SELECT 4, 3, 0

SELECT * 
FROM @User u
JOIN @UserSkill us
    ON u.id = us.[user_id]
JOIN @Skill s
    ON us.skill_id = s.id

DECLARE @skill_string VARCHAR(1000)

SELECT eu.name AS [Mentee]
    , ru.name AS [Mentor]
    , COUNT(*) AS [Commonality]
FROM @User eu -- 'eu' for mente'e' 'u'ser 
JOIN @UserSkill eus
    ON eu.id = eus.[user_id]
    AND eus.is_requesting = 1
JOIN @Skill es
    ON eus.skill_id = es.id
JOIN @UserSkill rus
    ON eus.skill_id = rus.skill_id
    AND rus.is_requesting = 0
JOIN @User ru -- 'r' for mento'r' 'u'ser 
    ON rus.[user_id] = ru.id
GROUP BY eu.name, ru.name
ORDER BY eu.name, ru.name
于 2012-04-05T15:21:01.607 回答