MySQL query from 3 tables at once

时间:2015-05-25 19:06:32

标签: mysql

I try to get data from 3 tables at once, tables are:

  • candidates
  • languages
  • candidates_language

Each of it represent some data about all users in my database.

As names suggest first table contains some general data like: firstname, surname, email.

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

Second one contains just languages_id and languageName

languages
    ---------------------------------
    | languageID |     languageName |
    ---------------------------------
    |      1     |      English     |
    ---------------------------------
    |      2     |      German      |
    ---------------------------------
    |      3     |      Spanish     |
    ---------------------------------

And last one try to connect all this together:

    candidates_language 
   (foreign keys: candidates_id to table candidates,languageID to languages)
        -----------------------------------------------------------------
        |   id   |   candidates_id  |  languageID |        skill        |
        -----------------------------------------------------------------
        |   1    |        22        |       1     |        basic        |
        -----------------------------------------------------------------
        |   2    |        22        |       3     |        basic        |
        -----------------------------------------------------------------
        |   3    |        23        |       1     |        advance      |
        -----------------------------------------------------------------
        |   4    |        23        |       2     |        basic        |
        -----------------------------------------------------------------

I need to connect results from this 3 tables for just some specific languages and make something like below:

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

I try to connect get to this point with code below::

SELECT
    candidates.firstname,
    candidates.surname,
    candidates_language.candidates_id,
    candidates_language.skill,
    languages.languageID
FROM
    candidates,
    candidates_language,
    languages

Unfortunately I am just far away from actual results I want. Is there a chance someone can help me with this?

3 个答案:

答案 0 :(得分:2)

如果语言数量预定义,那么您可以使用条件聚合来获得所需的结果集:

SELECT c.id, c.firstname, c.surname, c.email,
       MAX(CASE WHEN cl.languageID = 1 THEN cl.skill ELSE '-' END) AS 'English',
       MAX(CASE WHEN cl.languageID = 2 THEN cl.skill ELSE '-' END) AS 'German',
       MAX(CASE WHEN cl.languageID = 3 THEN cl.skill ELSE '-' END) AS 'Spanish'
FROM candidates AS c
LEFT JOIN candidates_language AS cl ON c.id = cl.candidates_id
GROUP BY c.id, c.firstname, c.surname, c.email

Demo Here

否则你必须使用dynamic sql

答案 1 :(得分:0)

由于语言不固定,您无法根据需要添加选择。

另一种选择是针对每种语言,您将获得一条记录。

    select candidates.id, candidates.firstname, candidates.surname, candidates.email, languages.languageName, candidates_language.skill from candidates_language inner join candidates.id on candidates_language.candidates_id inner join languages.id on candidates.languageID

答案 2 :(得分:-1)

内部联接应该适用于此。

您可以执行以下操作:

SELECT 
  c.firstname,
  c.surname,
  cl.candidates_id,
  cl.skill,
  l.languageID
FROM candidates c
INNER JOIN candidates_language cl
    on c.id = cl.candidates_id
INNER JOIN languages l
    on l.languageID = cl.languageID