3

我正在尝试使用关键字 AS 来组合两列,以便我可以对该列进行排序。

这是目前的完整查询。

$quotes = Quote::where('created_at', '>=', $date)
        ->where('created_at', '<=', date('Y-m-d').' 23:59:59')
        ->order_by('upvotes', 'desc')
        ->paginate(5);

我想要做

->order_by('(downvotes - upvotes) as votes', 'desc')

谢谢你。


解决方案

似乎使用 DB::raw() 是唯一的方法,Laravel/Eloquent 只是不理解 AS。

工作解决方案是

 $quotes = Quote::select(array('id', DB::raw('(downvotes - upvotes) as votes'), 'upvotes', 'downvotes', 'etc')) // Rest of column needed.
        ->where('created_at', '>=', $date)
        ->where('created_at', '<=', date('Y-m-d').' 23:59:59')
        ->order_by('votes', 'asc')
        ->paginate(5);
4

2 回答 2

2

尝试这个:

Quote::where('created_at', '>=', $date)
        ->select(array('id',DB::raw('(downvotes - upvotes) as votes'))) //and what you need
        ->where('created_at', '<=', date('Y-m-d').' 23:59:59')
        ->order_by('upvotes', 'desc')
        ->order_by('votes', 'desc')
        ->paginate(5);
于 2013-03-01T13:31:54.340 回答
0
$selectdata = DB::table('TableDetails')
            ->leftJoin('TableStatus', 'TableDetails.TableID', '=', 'TableStatus.TableID')
            ->leftJoin('LoginUser', 'TableStatus.WaiterLoginID', '=', 'LoginUser.LoginID')
            ->leftJoin('UserRole', 'LoginUser.UserRoleID', '=', 'UserRole.UserRoleID')
            ->select('LoginUser.UserName','UserRole.UserRoleName','TableStatus.TableStatus','TableStatus.TableTimeStatus','TableStatus.WaiterLoginID','TableDetails.TableSeatCount - TableStatus.TableSeatOccupied AS TableRemainingCount')
            ->get();

只需在减号之前和之后生活一个“空间”,它对我有用。

于 2015-06-16T04:48:36.680 回答