query sql结合多个表

时间:2013-09-01 04:38:02

标签: mysql 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  |
-------------------------------------------------------------------------------

非常感谢

1 个答案:

答案 0 :(得分: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