过滤mysql内部使用和连接一对多结果

时间:2012-05-15 13:14:28

标签: php mysql inner-join

我有两张桌子,例如

用户

----------------------------------
id  | name | blah
----------------------------------
1   | Samy | stackoverflow
2   | jhon | some thing
----------------------------------

技能

---------------------------------------
id | user_id | skill_title | level
---------------------------------------
1  | 1       | php         | good
2  | 1       | css         | excellent
3  | 1       | photoshop   | fair
4  | 2       | php         | good
---------------------------------------

我运行像这样的查询

SELECT * FROM users 
INNER JOIN skills ON users.id = skills.user_id
WHERE ($skill_title[0] LIKE 'skills_title' AND 
       $skill_title[1] LIKE 'skills_title')

其中$ skill_title是一个数组 我需要的是选择拥有所有这些技能的用户,即PHP,CSS 如果我像上面那样进行查询,它将永远不会带来数据,因为它将每一条记录与所有数组元素进行比较并且如果我更换并使用Or它将起作用但它不会带来具有所有技能的用户

任何想法?

3 个答案:

答案 0 :(得分:1)

@ joeshmo的回答可能是你最好的解决方案,但也值得尝试一下。如果您拥有大量用户可能会更快,但很少有具备您所需技能的用户:

SELECT * FROM users
WHERE
    id IN (SELECT user_id FROM skills WHERE skill_title = '$skills[0]')
    AND id IN (SELECT user_id FROM skills WHERE skill_title = '$skills[1]')
    ...

您的PHP脚本可能如下所示:

$skills = array("php", "css", ... ); // replace ... with the skills you are looking for
$whereClause = array();
for ($i=0, $count=count($skills); $i < $count; $i++)
    $whereClause[] = "id IN (SELECT user_id FROM skills WHERE skill_title = '{$skills[$i]}')";

$query = "SELECT * FROM users". (count($whereClause) > 0 ? " WHERE ". implode(" AND ", $whereClause) : "");

<小时/> 另一种可能的解决方案是使用多个INNER JOIN s,正如@joeshmo建议的那样。你可以通过这样做使它变得更小更清洁:

SELECT *
FROM
    users u
    INNER JOIN skills s1 ON u.id = s1.user_id AND s1.skill_title = '$skills[0]'
    INNER JOIN skills s2 ON u.id = s2.user_id AND s2.skill_title = '$skills[1]'
    ...

因此,您的PHP脚本可能如下所示:

$skills = array("php", "css", ... );
$joins = array();
for ($i=0, $count=count($skills); $i < $count; $i++)
    $joins[] = "INNER JOIN skills s{$i} ON u.id = s{$i}.user_id AND s{$i}.skill_title = '{$skills[$i]}'";

$query = "SELECT * FROM users u ". implode(" ", $joins);

我会尝试两种解决方案,看看哪种解决方案对您的数据集效果更好。

答案 1 :(得分:0)

我想你想为你正在寻找的每项技能再次加入它。

SELECT * FROM users 
INNER JOIN skills as first_skill ON users.id = first_skill.user_id
INNER JOIN skills as second_skill ON users.id = second_skill.user_id
WHERE ($skill_title[0] LIKE first_skill.skill_title AND 
       $skill_title[1] LIKE second_skill.skill_title)

您的查询问题是它只加入技能表一次。您的查询的工作方式如下:

-----------------------------------------------------
id  | name | blah           | skill_title | level
-----------------------------------------------------
1   | Samy | stackoverflow  | php         | good
1   | Samy | stackoverflow  | css         | excellent
1   | Samy | stackoverflow  | photoshop   | fair
2   | jhon | some thing     | php         | good
-----------------------------------------------------

没有行同时拥有css和php。我的查询是这样的:

----------------------------------------------------------------------------------------------------------------
id  | name | blah           | first_skill.skill_title | first_skill.level | second_skill.skill_title | second_skill.level |
----------------------------------------------------------------------------------------------------------------
1   | Samy | stackoverflow  | php                     | good              | php                      | good               |
1   | Samy | stackoverflow  | php                     | good              | css                      | excellent          |
1   | Samy | stackoverflow  | php                     | good              | photoshop                | fair               |
... (there would be nine for Samy)
2   | jhon | some thing     | php                     | good              | php                      
----------------------------------------------------------------------------------------------------------------

第二行同时包含phpcss。那是匹配的。

这对于多达十项技能来说将是乏味的。你可以改为做多个查询。

答案 2 :(得分:0)

$skills = Array("php","css", ..... );
$query = "  select u.id, u.name from users u left join skills s on u.id=s.user_id "
        ."  where s.skill_title in (\"".implode($skills,"\",\"")."\") "
        ."  group by s.user_id having count(s.id) = ".count($skills);