-1

在下面的代码中,我使用了多个表连接,并且我在表 clients_topup 表中有多个具有相同 id 的行,而我需要从表中计算多行并显示来自其他两个表的所有数据

在下面的代码中,我只得到计数值如何计算出来

foreach ($clients as $client) {
            $clientsLevelOne = DB::table('clients')
            ->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
            ->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
            ->select(DB::raw("SUM(topup_amount) as count"))
            ->where('clients.under_reference','=',$client->reference_id)->get();        

            $users[] = $clientsLevelOne;
          }
4

1 回答 1

0

我认为你这样做:

  $client_id_list =  [];
  foreach ($clients as $client) {
      $client_id_list[] => $client->id,
  }

 $clientsLevelOne = DB::table('clients')
            ->select(DB::raw("SUM({table_name}.topup_amount) as count"))
            ->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
            ->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
            ->where('clients.under_reference','=',$client->reference_id)
            ->whereIn('client_id', $client_id_list)
            ->groupBy('client_id')
            ->get();
于 2019-12-13T09:42:16.587 回答