假设我有两个这样的表:
--------+-------
| id | name |
--------+-------
| 1 | Paul |
| 2 | Jack |
| 3 | Joe |
--------+-------
--------+--------
| id | Color |
--------+--------
| 1 | Blue |
| 2 | Red |
| 3 | Pink |
--------+--------
我想要这样的结果:
--------+-----------------
| Paul | Blue,Red,Pink |
--------+-----------------
我不知道它是否取决于复杂的查询或我可能错过的“简单”关键字。我在 JOIN 附近搜索过这样的事情:
SELECT * FROM main WHERE name = Paul CROSS JOIN colors WHERE 1;
但这似乎并不那么容易,而且当然行不通。
任何要搜索的提示或关键字将不胜感激
最佳答案
使用交叉联接
和group_concat
。
查询
select t1.name,group_concat(distinct t2.color separator ',') as color
from tbl1 t1,tbl2 t2
where t1.name = 'Paul'
group by t1.name;
关于mysql - 将整个表连接为单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32745656/