Latest

Monday, July 10, 2017

how do I count duplicates and order by most duplicates

Asked by: maxit


how do I count duplicates and order by most duplicates and also show how many duplicates there are?

I have a table with some numbers and would like to sort numbers after duplicates and also for each number print how many duplicates it had.

How can I do that?

My table structure:

    Schema::create('random_numbers', function (Blueprint $table) {
        $table->bigincrements('id');
        $table->biginteger('number')->unsigned();
        $table->timestamps();
    });

Answers

Answered by: Sagar Gautam at 2017-07-10 09:25PM Accepted



You can do it with laravel raw query like this:

DB::table('random_numbers')
   ->select('random_numbers.*',DB::raw('COUNT(number) as count'))
   ->groupBy('number')
   ->orderBy('count')
   ->get();

This might work.




Source

No comments:

Post a Comment

Adbox