我想建立一个查询。
here is table:
id user_id sn_no
1 22 00112233
2 22 987654325
3 22 65489732
4 25 984123123
5 25 9568456456
6 25 65456456
我想要这样的结果:
{
"user_id":22,
"sn_no": "00112233,987654325,65489732"
}
{
"user_id":25,
"sn_no": "984123123,9568456456,65456456"
}
任何人都可以帮忙解决这个问题吗?
我试过:
concat
, GROUP_CONCAT
但无法得到结果。你能帮我在加入查询中添加这个吗?
$users = \DB::table('users')
->join('users_mcu', 'users.id', '=', 'users_mcu.user_id')
->join('country_user', 'users.id', '=', 'country_user.user_id')
->join('country_phase_color', 'country_user.country_id', '=', 'country_phase_color.id')
->select('users.id', 'users.first_name', 'users.last_name', 'users.company', 'users.designation', 'users.lang', 'users.phone', 'users.disp_graph', 'users.user_image', 'users.email', 'users.role', 'users.created_at', 'users.updated_at', 'country_user.country_id', 'country_phase_color.country_name')
->get();
最佳答案
尝试这个-
YourModel::select('user_id')
->selectRaw('GROUP_CONCAT(sn_no) as sn')
->groupBy('user_id')
->get();
关于php - 两列 where 子句用逗号合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60388981/