2015-05-25 98 views
1

我嘗試一次得到3個表數據,表是:MySQL查詢一次

  • 候選人
  • 語言
  • candidates_language

每它代表的一些有關我數據庫中所有用戶的數據。

由於姓名建議,第一張表包含一些常規數據,例如名字,姓氏,電子郵件。

candidates 
     ------------------------------------------------------------------------ 
     | id |  firstname |  surname  |  email  | 
     ------------------------------------------------------------------------ 
     | 22 |  John  |  Doe   | [email protected]  | 
     ------------------------------------------------------------------------ 
     | 23 |  Peter  |  Miller  | [email protected]  | 
     ------------------------------------------------------------------------ 

第二個只包含languages_id和languageName

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

而最後一個嘗試連接這一切在一起:

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  | 
     ----------------------------------------------------------------- 

我需要的結果從僅僅是這個3個表連接具體語言如下:

--------------------------------------------------------------------------- 
| id | firstname | surname  |  email  | english | german | 
--------------------------------------------------------------------------- 
| 22 | John  |  Doe  | [email protected] | basic | - | 
--------------------------------------------------------------------------- 
| 23 | Peter  |  Miller  | [email protected] | advance | basic | 
--------------------------------------------------------------------------- 

我嘗試連接到這一點下面::

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

不幸的是我只是遠離實際結果我想要的代碼。有沒有機會可以幫助我呢?

+0

你想爲你的語言表中的每個可用語言添加一列嗎?還是隻想爲特定語言添加列? – RavB

+0

嗨RavB, 我只需要特定語言的列.btw在你的筆記上編輯我的問題庫 – Andurit

回答

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

+0

這看起來很棒! – Andurit

-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 
+0

嗨patmcc, 感謝您的評論,不幸的是這使得用戶:)雙重性 – Andurit

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