在应用GroupBy Laravel时,OrderBy无法正常工作

时间:2016-09-14 10:05:02

标签: php mysql laravel group-by sql-order-by

我正在使用以下查询来检索用户订阅源,但是当我们通过desc应用订单created_at time时,订购最新更新无效

这里我给出了SQL查询。请建议我解决我的问题的解决方案

    select `users`.`id` as `uid`, `updates`.`post_id`, `updates`.`id` as `upid`, `friends`.`id` as `fid`, `updates`.`user_id`, `updates`.`privacy`, `updates`.`updated_at`, `updates`.`created_at`, `friends`.`first_user_id`, `friends`.`second_user_id`, `friends`.`friend_status`, `updates`.`update_type` from `updates` 
inner join `users` on `updates`.`user_id` = `users`.`id`
inner join `friends` on 
    CASE
        WHEN friends.first_user_id = 1
        THEN friends.second_user_id = updates.user_id
        WHEN friends.second_user_id= 1
        THEN friends.first_user_id = updates.user_id
    END
where `friends`.`friend_status` > 0 
    and `updates`.`privacy` in (1,2) 
    and `updates`.`user_id` != 1 and 
    `updates`.`deleted_at` is null 
group by `updates`.`post_id` 
order by `updates`.`created_at` desc

Laravel代码

Update::select(
        'users.id as uid', 'posts.id as pid', 'updates.post_id', 'updates.id as upid',
        'friends.id as fid', 'updates.user_id', 'updates.privacy', 'updates.updated_at', 'updates.created_at', 'posts.deleted_at', 'friends.first_user_id', 
        'friends.second_user_id', 'friends.friend_status', 'updates.update_type', 'posts.wall_user_id'
        )->join('users', 'updates.user_id', '=', 'users.id')
        ->join('posts', 'posts.id', '=', 'updates.post_id')
        ->join('friends', function($join) use ($userId){
            $join->on(DB::raw('CASE
                        WHEN friends.first_user_id = '.$userId.'
                        THEN friends.second_user_id = updates.user_id
                        WHEN friends.second_user_id= '.$userId.'
                        THEN friends.first_user_id = updates.user_id
                        END'
                    ), DB::raw(''), DB::raw(''));
        })->where('friends.friend_status', '>', 0)
        ->whereIn('updates.privacy', [1,2])
        ->where('updates.user_id', '!=', Auth::user()->id)
        ->groupBy('updates.post_id')
        ->orderBy('updates.created_at', 'DESC')
        ->get();

1 个答案:

答案 0 :(得分:3)

select * from(select `users`.`id` as `uid`, `updates`.`post_id`, `updates`.`id` as `upid`, `friends`.`id` as `fid`, `updates`.`user_id`, `updates`.`privacy`, `updates`.`updated_at`, `updates`.`created_at`, `friends`.`first_user_id`, `friends`.`second_user_id`, `friends`.`friend_status`, `updates`.`update_type` from `updates` 
inner join `users` on `updates`.`user_id` = `users`.`id`
inner join `friends` on 
    CASE
        WHEN friends.first_user_id = 1
        THEN friends.second_user_id = updates.user_id
        WHEN friends.second_user_id= 1
        THEN friends.first_user_id = updates.user_id
    END
where `friends`.`friend_status` > 0 
    and `updates`.`privacy` in (1,2) 
    and `updates`.`user_id` != 1 and 
    `updates`.`deleted_at` is null order by `updates`.`created_at` desc) as tmp_table 
group by `updates`.`post_id`