db->table('races r'); $builder->join('tracks t', 't.id = r.track_id'); $builder->select('r.track_id, COUNT(*) AS count, t.name AS track_name'); $builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto); $builder->whereIn('r.car_id', $carsCatIds); $builder->groupBy('r.track_id'); $builder->orderBy('count DESC'); $query = $builder->get(); if ($query && $query->getNumRows() > 0) $total = $query->getNumRows(); if ($total == 0) return [[], 0]; $builder = $this->db->table('races r'); $builder->join('tracks t', 't.id = r.track_id'); $builder->select('r.track_id, COUNT(*) AS count, t.name AS track_name'); $builder->where('UNIX_TIMESTAMP(r.timestamp) >', $backto); $builder->whereIn('r.car_id', $carsCatIds); $builder->groupBy('r.track_id'); $builder->orderBy('count DESC'); if ($limit > 0) $builder->limit($limit, $offset); $query = $builder->get(); if ($query && $query->getNumRows() > 0) $list = $query->getResult(); return [$list, $total]; } }