0

到目前为止,我正在处理我做过的最复杂的 SQL 查询,这对你们大多数人来说可能非常简单(:

我有三个表,用户、技能和 User_Skills。其中的领域应该是相当不言自明的。

我想选择具有一项或多项技能且符合我的标准的人。

我可以选择具有我所追求的技能的用户,但我不确定查询多个技能的语法。

我只想使用一个查询,所以我尝试使用 GROUP_CONCAT

这是我的 SQL:

SELECT User_id, first_name, last_name, county, GROUP_CONCAT(CAST(Skill_id AS CHAR))
FROM User LEFT JOIN User_Skills ON User.id = User_Skills.User_id
LEFT JOIN Skills ON User_Skills.Skill_id = Skills.id GROUP BY User_id

User_id  first_name  last_name  county        GROUP_CONCAT(CAST(Skill_id AS CHAR))
1000    Joe         Blow       West Yorkshire   8,6,1,9,7,3,5,10
1001    Fred        Bloggs     COUNTY1          5,8,2,7,9
1003    asdf        asdf1      Some County      10,8,2

如何将搜索限制为仅具有技能 5 和 9 的人?

4

5 回答 5

2
SELECT User_id, first_name, last_name, county, GROUP_CONCAT(CAST(Skill_id AS CHAR))
FROM User JOIN User_Skills ON User.id = User_Skills.User_id
JOIN Skills ON User_Skills.Skill_id IN (5,9)
GROUP BY User_id

如果你想防止多个技能记录,那么你可能需要一个子查询

SELECT User_id, first_name, last_name, county,
(
    SELECT (GROUP_CONCAT(CAST(subSkill.Skill_id AS CHAR))
    FROM Skills as subSkill WHERE subSkill.skill_id = User_Skills.skill_id
    GROUP BY subSkill.skill_id
)
FROM User JOIN User_Skills ON User.id = User_Skills.User_id
JOIN Skills ON User_Skills.Skill_id IN (5,9)
GROUP BY User_id
于 2009-12-21T14:56:23.303 回答
0

为什么不能只使用 INNER JOIN 代替 LEFT 连接,并为所需技能发出 WHERE 子句?

SELECT User_id, first_name, last_name, county, GROUP_CONCAT(CAST(Skill_id AS CHAR)) 
FROM User 
INNER JOIN User_Skills ON User.id = User_Skills.User_id 
INNER JOIN Skills ON User_Skills.Skill_id = Skills.id GROUP BY User_id
WHERE skill_id = 5 OR skill_id=9
于 2009-12-21T14:56:10.560 回答
0

以下是方法(将为您提供技能 5 和 9 的用户):

SELECT User_id, first_name, last_name, county FROM User INNER JOIN User_Skills ON User.id = User_Skills.User_id INNER JOIN Skills ON User_Skills.Skill_id = Skills.id WHERE Skills.Skill_id = 5 AND Skills.Skill_id = 9 GROUP BY User_id
于 2009-12-21T14:57:24.380 回答
0
SELECT  *
FROM    users u
WHERE   EXISTS
        (
        SELECT  NULL
        FROM    user_skills us
        WHERE   us.skill_id IN (5, 9)
                AND us.user_id = u.id
        LIMIT 1, 1
        )

(user_id, skill_id)这依赖于一个PRIMARY KEY或一个键的事实UNIQUE(即您不能多次将技能分配给用户)。

更新:

返回所有技能(逗号分隔):

SELECT  u.*,
        (
        SELECT  GROUP_CONCAT(skill_id)
        FROM    user_skills uso
        WHERE   uso.user_ud = u.user_id
        ) AS all_skills
FROM    users u
WHERE   EXISTS
        (
        SELECT  NULL
        FROM    user_skills us
        WHERE   us.skill_id IN (5, 9)
                AND us.user_id = u.id
        LIMIT 1, 1
        )
于 2009-12-22T15:58:25.683 回答
0

你只需要一个

HAVING FIND_IN_SET('5', skills) AND FIND_IN_SET('9', skills)

在 group by 之后(其中“skills”是分组列的名称)。所以你的查询现在应该看起来像

SELECT User_id, first_name, last_name, county, GROUP_CONCAT(CAST(Skill_id AS CHAR)) as skills
FROM User LEFT JOIN User_Skills ON User.id = User_Skills.User_id
LEFT JOIN Skills ON User_Skills.Skill_id = Skills.id GROUP BY User_id
HAVING FIND_IN_SET('5', skills) AND FIND_IN_SET('9', skills)
于 2009-12-23T17:00:28.387 回答