2016-04-29 58 views
1

我需要的信息從兩個表合併成一個表,下表是從兩個表中的信息合併到一個表

table 1 
+----+---------------+---------+ 
|id_k| name  | value | 
+----+---------------+---------+ 
| 1 | enak   | 4  | 
| 2 | nor   | 3  | 
+----+---------------+---------+ 

table 2 
+------+------+---------+ 
| id_d | id_k | feel | 
+------+------+---------+ 
| 1 | 1 | good | 
| 2 | 1 | better | 
| 3 | 1 | verygood| 
+------+------+---------+ 

結果應該是

+------+------+-------+------------------------+ 
| id_d | name | value |   feel  | 
+------+------+-------+------------------------+ 
| 1 | enak | 4 | good, better, verygood | 
| 2 | nor | 3 |      | 
+------+------+-------+------------------------+ 

這是我的代碼[不工作]

select k.name, k.value, s.feel 
from table1 as k 
left join table2 as s on s.id_k=k.id_k 
+0

(我敢問)你爲什麼要格式化這樣的結果呢? – Strawberry

回答

1

您可以使用gorup_concat函數來協調來自tabl的值E2在結果昏迷分隔的字符串:

SELECT table1.id_k, name, value, GROUP_CONCAT(feel SEPARATOR ', ') AS feel 
FROM  table1 
JOIN  table2 ON table1.id_k = table2.id_k 
GROUP BY table1.id_k 
相關問題