Home > Net >  Get the first data that does not occur twice
Get the first data that does not occur twice

Time:11-03

How can I write a query that will get the first active game_id that does not occur twice in the table below?

CodePudding user response:

This will only show all the game id and the number of their occurences

$user_info = DB::table('usermetas')
                 ->select('browser', DB::raw('count(*) as total'))
                 ->groupBy('browser')
                 ->get();

CodePudding user response:

How about :

DB::table('tablename')
    ->select(DB::raw('COUNT(game_id) as totalgames, game_id'))
    ->where('is_active', 1)
    ->groupBy('game_id')
    ->havingRaw('COUNT(game_id) = 1')
    ->first();

CodePudding user response:

Do you want to do this? Such a solution could be:

$data = DB::table('your_table')
              ->groupBy('game_id')
              ->select('id')
              ->toArray();
$result = DB::table('your_table')
              ->whereNotIn($data)
              ->where('is_active', 1)
              ->first();
  • Related