Mysql SELECT仅显示1个结果,而不是3个表中的多个结果

时间:2015-06-02 08:30:53

标签: mysql

我尝试创建一个SQL请求,从中获取多个表中的所有数据。当一个用户具有例如更多“强制执行”时问题开始。但首先让我们看看代码:

SELECT c.id, c.firstname, c.surname, c.email, c.process, c.search_work, c.note,
               MAX(CASE WHEN cl.languageID = 1 THEN cl.skill ELSE '-' END)AS 'en',
               MAX(CASE WHEN cl.languageID = 2 THEN cl.skill ELSE '-' END)AS 'ge',
               ce.enforcement       
        FROM candidates AS c
        LEFT JOIN candidates_language AS cl ON c.id = cl.candidates_id
        LEFT JOIN candidates_enforcement as ce on c.id = ce.candidates_id
        GROUP BY c.id, c.firstname, c.surname, c.email

从这里可以看到,我在候选ID上使用外键搜索多个表。

对于这个purpouse,这里有两个表:

candidates

        ------------------------------------------------------------------------
        |   id   |     firstname    |      surname       |        email        |
        ------------------------------------------------------------------------
        |   22   |      John        |        Doe         |    john@doe.com     |
        ------------------------------------------------------------------------
        |   23   |      Peter       |       Miller       |    doe@john.com     |
        ------------------------------------------------------------------------

candidates_enforcement

        --------------------------------------------------
        |   id   |   candidates_id  |    enforcement     |
        --------------------------------------------------
        |    1   |        22        |       Advocate     |
        --------------------------------------------------
        |    2   |        22        |      Programmer    |
        --------------------------------------------------
        |    3   |        23        |       IT Admin     |
        --------------------------------------------------
candidate_id =候选人的外键。使用上面的SQL请求,结果应如下所示:

---------------------------------------------------------------------------------
| id |     firstname    |      surname       |        email        | enforcement
---------------------------------------------------------------------------------
| 22 |      John        |        Doe         |    john@doe.com     | Advocate, Programmer |
---------------------------------------------------------------------------------
| 23 |      Peter       |       Miller       |    doe@john.com     | IT Admin  
---------------------------------------------------------------------------------

不幸的是,它始终只显示“强制执行”的结果。因此对于id为22的cancidate,它是Advocate而不是Advocate,程序员 是否有人可以帮助我找到解决方法? 感谢

P.S。关于FIDDLE的工作演示 http://sqlfiddle.com/#!9/25b1b/1

3 个答案:

答案 0 :(得分:1)

您可以像这样使用GROUP_CONCAT:

SELECT
    candidates.id,
    candidates.firstname,
    candidates.surname,
    candidates.email,
    group_concat(DISTINCT candidates_enforcement.enforcement)
FROM
    candidates
    LEFT JOIN candidates_enforcement
        ON candidates.id = candidates_enforcement.candidates_id
GROUP BY
    candidates.id,
    candidates.firstname,
    candidates.surname,
    candidates.email

参考:

答案 1 :(得分:1)

您可以将Group_Concat与Distinct选项一起使用

SELECT c.id,c.firstname,c.surname,c.email,group_concat(distinct ce.enforcement)
FROM candidates c
LEFT JOIN candidates_enforcement ce
ON c.id=ce.candidates_id
GROUP BY c.id,c.firstname,c.surname,c.email

distinct选项将帮助您删除冗余值。

答案 2 :(得分:0)

据我所知,你希望当一个人有两个执法时,两个执法都会出现。这里的问题是你LEFT将candidate_enforcement表加入候选表。 LEFT join确实是从候选表中获取元组并从candidate_enforcement加入相应的元组。候选人表中只有一个候选人元组。所以它只显示一次候选者执行是否有针对该特定候选人的许多元组。  要更正此问题,请执行RIGHT JOIN。或者你可以做同样的LEFT JOIN,交换表格。

SELECT c.id, c.firstname, c.surname, c.email,ce.enforcement
FROM candidates AS c
RIGHT JOIN candidates_enforcement as ce on c.id = ce.candidates_id