我有这样的表
表1
nid | name
3 john
表2
tid | address | fkey_id
56 city 3
57 street 3
结果
name | address
约翰城
街
我怎样才能得到这样的结果 我尝试下面的代码
SELECT name,address
FROM tbl_name
INNER JOIN tbl_address
ON tbl_name.nid = tbl_address.fkey_id';
结果 我得到了json
{
"name": "john",
"address": "city"
},
{
"name": "john",
"address": "street"
},
但我希望像这样的json结果
{
"name": "john",
"address": "street,city"
}
请帮帮我
答案 0 :(得分:0)
您需要的实际概念是GROUP_CONCAT
MySQL。
您需要的最终查询是
SELECT t1.name,t2.final_address
FROM tbl_name t1
INNER JOIN (
SELECT fkey_id,GROUP_CONCAT(address) as final_address
FROM tbl_address
GROUP BY fkey_id
) t2 ON t1.nid = t2.fkey_id
答案 1 :(得分:0)
一种选择是使用GROUP_CONCAT
:
SELECT t1.name, GROUP_CONCAT(t2.address) AS address
FROM tbl_name t1
INNER JOIN tbl_address t2
ON t1.nid = t2.fkey_id
GROUP BY t1.name
在这里演示: