gpt4 book ai didi

php - 如何在从数据库中检索数据时隐藏重复数据

转载 作者:行者123 更新时间:2023-11-29 02:39:47 28 4
gpt4 key购买 nike

我有这个功能:

public function readData(){
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
->select(
'users.name as name',
'group_user.user_id as id',
'groups.name as groupname',
'meetings.owned_by_id as meetings'
)
->groupBy('users.id')
->get();

我收到这个错误:

enter image description here

当我删除 groupBy 部分时,它可以工作,但会出现像这样的重复数据它返回这样的数据:

[{"name":"Mohamed Adel","id":6,"groupname":"Team Mohamed","meetings":6},{"name":"Mohamed Adel","id":6,"groupname":"Team Mohamed","meetings":6},{"name":"Norman Osborn","id":2,"groupname":"Team Mohamed","meetings":2},{"name":"Harry Osborn","id":3,"groupname":"Team Harry","meetings":3}]

谢谢。

最佳答案

使用这个

public function readData()
{
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
->select(
'users.id as user_id',
'users.name as name',
'group_user.user_id as id',
'groups.name as groupname',
'meetings.owned_by_id as meetings'
)
->groupBy('user_id')
->get();

将 config\database.php 更改为

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false, // must be false
'engine' => null
],

关于php - 如何在从数据库中检索数据时隐藏重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090938/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com