SQL服务器匹配列上的两个表

时间:2017-03-03 13:50:40

标签: sql sql-server select join

我有两个表,一个存储用户技能,另一个存储工作所需的技能。我想匹配每个用户与工作匹配的技能数量。 表结构是

Table1: User_Skills
| ID | User_ID | Skill    |
---------------------------
| 1  |  1      |  .Net    |
---------------------------
| 2  |  1      |  Software|
---------------------------
| 3  |  1      |  Engineer|
---------------------------
| 4  |  2      |  .Net    |
---------------------------
| 5  |  2      |  Software|
---------------------------

Table2: Job_Skills_Requirement
| ID | Job_ID | Skill     |
--------------------------
| 1  |  1      |  .Net    |
---------------------------
| 2  |  1      |  Engineer|
---------------------------
| 3  |  1      |  HTML    |
---------------------------
| 4  |  2      |  Software|
---------------------------
| 5  |  2      |  HTML    |
---------------------------

我试图用逗号分隔技能并进行比较,但这些技能可能有不同的顺序。

修改 这里的所有答案都很棒。我正在寻找的结果是将所有作业与所有用户匹配,稍后我将匹配其他属性。

3 个答案:

答案 0 :(得分:6)

您可以通过skill列加入表并计算匹配项:

SELECT   user_id, job_id, COUNT(*) AS matching_skills
FROM     user_skills u
JOIN     job_skills_requirement j ON u.skill = j.skill
GROUP BY user_id, job_id

编辑:
如果您还想显示没有匹配技能的用户和工作,您可以使用full outer join代替。

SELECT          user_id, job_id, COUNT(*) AS matching_skills
FROM            user_skills u
FULL OUTER JOIN job_skills_requirement j ON u.skill = j.skill
GROUP BY        user_id, job_id

编辑2:
正如Jiri Tousek评论的那样,上述查询将产生null s,其中用户和作业之间没有匹配。如果你想在它们之间使用完整的笛卡尔产品,你可以使用(滥用?)cross join语法并计算每个用户和每个工作之间实际匹配的技能数量:

SELECT     user_id, 
           job_id,
           COUNT(CASE WHEN u.skill = j.skill THEN 1 END) AS matching_skills
FROM       user_skills u
CROSS JOIN job_skills_requirement j
GROUP BY   user_id, job_id

答案 1 :(得分:2)

WITH User_Skills(ID,User_ID,Skill)AS(
   SELECT 1,1,'.Net' UNION ALL
   SELECT 2,1,'Software' UNION ALL
   SELECT 3,1,'Engineer' UNION ALL
   SELECT 4,2,'.Net' UNION ALL
   SELECT 5,2 ,'Software'
),Job_Skills_Requirement(ID,Job_ID,Skill)AS(
   SELECT 1,1,'.Net' UNION ALL
   SELECT 2,1,'Engineer' UNION ALL
   SELECT 3,1,'HTML' UNION ALL
   SELECT 4,2,'Software' UNION ALL
   SELECT 5,2 ,'HTML'
),Job_User_Skill AS (
   SELECT j.Job_ID,u.User_ID,u.Skill
   FROM Job_Skills_Requirement AS j INNER JOIN User_Skills AS u ON u.Skill=j.Skill
)
SELECT jus.Job_ID,jus.User_ID,COUNT(jus.Skill),STUFF(c.Skills,1,1,'') AS Skill
FROM Job_User_Skill AS jus
CROSS APPLY(SELECT ','+j.Skill FROM Job_User_Skill AS j WHERE j.Job_ID=jus.Job_ID AND j.User_ID=jus.User_ID FOR XML PATH('')) c(Skills)
GROUP BY jus.Job_ID,jus.User_ID,c.Skills
ORDER BY jus.Job_ID
Job_ID      User_ID                 Skill
----------- ----------- ----------- -------------
1           1           2           .Net,Engineer
1           2           1           .Net
2           1           1           Software
2           2           1           Software

答案 2 :(得分:2)

如果您想匹配所有用户和所有作业,那么Mureinik的优秀答案就不正确了。

您需要先生成所有行,然后使用cross join生成所有行,然后计算匹配的行:

select u.user_id, j.job_id, count(jsr.job_id) as skills_in_common
from users u cross join
     jobs j left join
     user_skills us
     on us.user_id = u.user_id left join
     Job_Skills_Requirement jsr
     on jsr.job_id = j.job_id and
        jsr.skill = us.skill
group by u.user_id, j.job_id;

注意:这假定存在usersjobs表。您当然可以使用子查询生成这些。