Php 拉威尔群与和有许多关系

Php 拉威尔群与和有许多关系,php,mysql,laravel,Php,Mysql,Laravel,我有一个users表,表中有一列country,用户属于任何类别。用户还与支付表有许多关系 我正在努力: 按国家对用户进行分组,显示每个国家最流行的类别(此问题已解决) 对每个分组国家的付款表中的金额列求和 每次我尝试加入付款表时,它都会返回多个结果,这使得总和完全错误。我认为我没有使用正确的查询 以下是表格结构: 用户 id | name | country 1 | UserA | Canada 2 | UserB | USA 3 | UserC | Canada id | amo

我有一个
users
表,表中有一列
country
,用户
属于任何类别。用户还与
支付表有许多关系

我正在努力:

  • 按国家对用户进行分组,显示每个国家最流行的类别(此问题已解决)
  • 对每个分组国家的
    付款
    表中的
    金额
    列求和
  • 每次我尝试加入
    付款
    表时,它都会返回多个结果,这使得总和完全错误。我认为我没有使用正确的查询

    以下是表格结构:

    用户

    id | name  | country
    
    1  | UserA | Canada
    2  | UserB | USA
    3  | UserC | Canada
    
    id | amount | user_id
    1  | 500    | 1
    2  | 200    | 2
    3  | 150    | 1
    4  | 100    | 3
    
    类别

    id | Name
    1  | Housing
    2  | Cooking
    
    类别用户

    id  | category_id | user_id
    1   | 1           | 1
    2   | 2           | 2
    3   | 1           | 3
    
    付款

    id | name  | country
    
    1  | UserA | Canada
    2  | UserB | USA
    3  | UserC | Canada
    
    id | amount | user_id
    1  | 500    | 1
    2  | 200    | 2
    3  | 150    | 1
    4  | 100    | 3
    
    使用以下代码解决了第一个问题:

    return User::leftJoin('category_user as cu', 'users.id', '=', 'cu.user_id')
            ->join('categories as c', 'cu.category_id', '=', 'c.id')
            ->groupBy(['country', 'c.name'])
            ->get([
                'users.country',
                'c.name',
                DB::raw('count(*) as total')
            ])
            ->groupBy('country')
            ->values()
            ->map(function (Collection $elt) {
                return $elt->sortByDesc('total')->first();
            });
    
    其结果如下:

    [
      {
        "country": "Canada",
        "name": "Housing",
        "total": 2
      },
      {
        "country": "USA",
        "name": "Cooking",
        "total": 1
      }
    ]
    
    我想从付款中得到总付款额。任何帮助都将不胜感激


    谢谢。

    我找不到减少查询数量的方法,但下面是我的本地测试

    public function foobar()
    {
        $popular = User::leftJoin('category_user as cu', 'users.id', '=', 'cu.user_id')
            ->join('categories as c', 'cu.category_id', '=', 'c.id')
            ->groupBy(['country', 'c.name'])
            ->get([
                'users.country',
                'c.name',
                DB::raw('count(*) as total')
            ])
            ->groupBy('country')
            ->values()
            ->map(function (Collection $elt) {
                return $elt->sortByDesc('total')->first();
            }); // this one is same (to get popular category)
    
        // this one for payments and number of users from that country
        return User::leftJoin('payments as p', 'users.id', '=', 'p.user_id')
            ->groupBy('country')
            ->get(['country', DB::raw('sum(p.amount) as total'), DB::raw('count(distinct(users.id)) as userCount')])
            ->map(function ($col) use ($popular) { // merging two collections into one
                $col->name = $popular->where('country', $col->country)->first()->name;
    
                return $col;
            });
    }
    
    它打印出这样的东西

    [
      {
        "country": "Canada",
        "total": "1150",
        "userCount": 2,
        "name": "Housing"
      },
      {
        "country": "Italy",
        "total": "100",
        "userCount": 1,
        "name": "Kitchen"
      },
      {
        "country": "USA",
        "total": "200",
        "userCount": 1,
        "name": "Cooking"
      }
    ]
    

    @您的输入将非常感谢。
    sum
    s是否与类别相关?它将是加拿大的
    总和
    还是加拿大住房的总和?仅针对该国。例如:
    sum of canada
    还有一个问题,
    total
    total number of payment
    还是
    total number of users in the top category
    ?由于一个用户可以在相同/不同的类别中进行多次付款,因此不清楚在这种情况下的付款总额是多少?好的,我需要查看1.)每个国家,2.)该国的用户总数,3.)该国最受欢迎的类别和4.)该国的付款总额。非常感谢!我真的非常感谢你的帮助。