Laravel Union操作后排序错误的问题 Fix the wrong order after using union method in Laravel query

解决方法,在两个子查询中分别加上limit即可。可以使用较大的数确保所有记录返回。

The solution is to use the limit method on the two subqueries. You can use a larger number to ensure that all records are returned.

$orders1 = Order::where('id', '=', $user->id)
    ->where('status', '!=', 0)
    ->orderBy('id', 'asc')
    ->limit(1e8);

$orders2 = Order::where('id', '=', $user->id)
    ->where('status', '=', 0)
    ->orderBy('created_at', 'desc')
    ->limit(1e8)
    ->unionAll($orders1)
    ->get();

 

Laravel Union操作后排序错误的问题 Fix the wrong order after using union method in Laravel query》有2个想法

  1. Ramon

    Getting all the rows and counting total is memory inefficient. So I used following approach to get total count. together and got right result under laravel 5.7.20. This will be better than merging collections then paginate which will not work on big amount of data.

    回复

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注