我如何计算大多数重复的重复和排序,并显示有多少重复?
我有一个带有一些数字的表格,并且希望在重复之后对数字进行排序,并且对于每个数字打印它有多少重复数字。
我该怎么做?
我的表结构:
Schema::create('random_numbers', function (Blueprint $table) {
$table->bigincrements('id');
$table->biginteger('number')->unsigned();
$table->timestamps();
});
答案 0 :(得分:4)
您可以使用laravel raw查询来执行此操作:
DB::table('random_numbers')
->select('random_numbers.*',DB::raw('COUNT(number) as count'))
->groupBy('number')
->orderBy('count')
->get();
这可能有用。