用户亲和力匹配SQL查询?

时间:2012-04-05 12:33:58

标签: sql sql-server

所以我有两个用户是导师和受指导者。

我希望向受指导者展示基于匹配技能的导师名单。

因此,注册将是一堆复选框,说...

受让人看到:

我需要帮助

[] Branding
[] Marketing
[] Legal stuff

导师看到:

我的专业知识

[] Branding
[] Marketing
[] Legal stuff

是否有基于最常见性的SQL服务器查询来匹配这些人?

希望这一切都有意义:\

干杯

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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