2013-09-01 74 views
0

我有一個表,如下查詢SQL合併多個表

  • idbook
  • idapplicant
  • idsiteplan
  • 日期

申請人

  • idapplicant
  • name_applicant

siteplan
  • idsiteplan
  • 計劃

詳細
  • iddetail
  • idbook

如何實現查詢,使之成爲如下

------------------------------------------------------------------------------- 
| name_applicant| siteplan| date  | detail        | 
------------------------------------------------------------------------------- 
| aaa   | tapak1 | 12-12-2013 | name_of_detail_1 : value_of_detail_1 | 
|          name_of_detail_2 : value_of_detail_2 | 
|          name_of_detail_3 : value_of_detail_3 | 
|          name_of_detail_4 : value_of_detail_4 | 
------------------------------------------------------------------------------- 
| bbb   | tapak2 | 13-12-2013 | name_of_detail_1 : value_of_detail_1 | 
|          name_of_detail_2 : value_of_detail_2 | 
|          name_of_detail_3 : value_of_detail_3 | 
|          name_of_detail_4 : value_of_detail_4 | 
------------------------------------------------------------------------------- 

非常感謝

+0

您還可以翻譯結果列名嗎?謝謝! :) –

+0

谷歌'MySQL加入'並且自己做 – Itay

回答

0

這會給你我認爲一個好的起點:

SELECT a.name_applicant, 
     s.plan as siteplan, 
     b.date, 
     CONCAT(d.name,' : ',d.value) as detail 
FROM applicant a 
INNER JOIN book b ON b.idapplicant = a.idapplicant 
INNER JOIN siteplan s ON s.idsiteplan = b.idsiteplan 
INNER JOIN detail d ON b.idbook = d.idbook