我有2张桌子
users_table
id | first_name
1 | first name1
2 | first name2
addresses_table
id | user_id | address
1 | 1 | first address of user 1
2 | 1 | second address of user 1
3 | 2 | first address of user 2
4 | 2 | second address of user 2
我想要一个像这样的表结果,第一列是users_table first_name,第二列是用逗号分隔的每个用户的爱好
<table>
<tr>
<td>first name1</td><td>first address of user 1, second address of user 1</td>
</tr>
<tr>
<td>first name2</td><td>first address of user 2, second address of user 2</td>
</tr>
</table>
如果我必须查询它将必须select * from users_table
然后得到每个用户的地址,如select * from addresses_table where user_id = 1
这不是非常有效,这可以通过ORM防止,但我想知道我们可以防止使用SQL
答案 0 :(得分:0)
您可以使用以下查询
选择first_name,group_concat(地址,',')作为地址 从 users_table usrTable 加入 usrTable.id = addTable.user_id上的addresses_table addTable group by first_name