我有这两张桌子:
语言
id | name
1 | java
2 | php
3 | c++
4 | javascript
开发
id|name
1 | John
2 | Jack
和关联
lang_dev
id|dev_id|lang_id
1| 1 | 1
2| 1 | 2
3| 2 | 3
4| 2 | 4
我可以使用以下内容加入他们:
SELECT dev.*, lang.name as lang_name
FROM developers as dev
LEFT JOIN lang_dev as ld ON dev.id = ld.dev_id
LEFT JOIN languages as lang ON lang.id = ld.tag_id
WHERE dev.id = 1
返回:
id| name | lang_name |
1 | john | java |
1 | john | php |
我想要的是获得一个如此独特的行:
id | name | lang_name|
1 | john | java, php|
如果有可能的话,我想把它放在一个数组中:
Array {
["id"]=>
string() "1"
["name"]=>
string() "john"
["lang_name"]=> Array {
string() "java"
string() "php"
}
}
答案 0 :(得分:0)
group_concat
应该这样做:
SELECT name, GROUP_CONCAT(lange_name)
FROM developers d
JOIN lang_dev ld ON d.id = ld.dev_id
JOIN languages l ON ld.lang_id = l.id
GROUP BY name