concat和group concat来自与另一个表连接的表

时间:2016-03-27 08:06:55

标签: mysql

如何从与另一个表连接的表中使用concat和group concat。 架构看起来像这样:

第一表:

MariaDB [ittresnamuda]> select * from tb_tipe_request;
+---------+------------+
| id_tipe | nama_tipe  |
+---------+------------+
|       1 | Perbaikan  |
|       2 | Permintaan | 
+---------+------------+

2行(0.00秒)

SECOND TABLE

MariaDB [ittresnamuda]> select a.ID_REQUEST, a.CATATAN from tb_requestfix a;
+------------+---------------------------------+
| ID_REQUEST | CATATAN                         |
+------------+---------------------------------+
|          3 | Akan kami cek jaringan tersebut |
|          4 | Iya, go ahead. Appproved        |
|          5 | Sudah di refill                 |
|         28 | Saja                            |
+------------+---------------------------------+
4 rows in set (0.00 sec)

第三张表

MariaDB [ittresnamuda]> select * from tb_link_tipe_request;
+----+------------+---------+
| id | id_request | id_tipe |
+----+------------+---------+
|  8 |          4 |       1 |
|  9 |          4 |       2 |
| 11 |          3 |       1 |
| 12 |          5 |       2 |
| 40 |         28 |       1 |
+----+------------+---------+
5 rows in set (0.00 sec)

我已经使用了join,concat和group_concat,但仍然没有结果。我需要选择这样的表格:

+------------+---------------------------------+------------------------+
| ID_REQUEST | CATATAN                         |  TIPE_REQUEST          |
+------------+---------------------------------+------------------------+
|          3 | Akan kami cek jaringan tersebut | Perbaikan              |
|          4 | Iya, go ahead. Appproved        | Perbaikan / Permintaan |
|          5 | Sudah di refill                 | Permintaan             |
|         28 | Saja                            | Perbaikan              |
+------------+---------------------------------+------------------------+

对于帮助,非常感谢。

1 个答案:

答案 0 :(得分:1)

您可以将所有表连接在一起,然后像这样使用GROUP_CONCAT

select a.ID_REQUEST, a.CATATAN ,group_concat(t.nama_tipe separator ',') as tipe_request
from tb_requestfix a
INNER JOIN tb_link_tipe_request at
 ON(a.id_request = at.id_request)
INNER JOIN tb_tipe_request t
 ON(t.id_tipe = at.id_tipe)
GROUP BY a.id_request