如何在计数查询中使用“AS”?

时间:2018-06-08 11:39:33

标签: mysql laravel laravel-5.3

如何在计数查询中使用 AS

实际上我希望json结果像public function firstHourTrades(){ $user_id = Auth::user()->id; $data = DB::table('finaltrade') ->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id') ->where('finaltrade.user_id', $user_id) ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time')) ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')")) ->count(); return response()->json($data); } 一样。我不知道该怎么称呼这个东西?

HttpSession sesion = request.getSession();
List<String> dbsurnames = new ArrayList<String>();
while(rs.next()) { 
    dbsurnames.add(rs.getString("Surnames")); 
}
sesion.setAttribute("Surnames", dbsurnames);

1 个答案:

答案 0 :(得分:3)

您还可以执行原始选择并手动分配别名:

$data = DB::table('finaltrade')
    ->select(DB::raw('count(*) as count'))
    ->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
    ->where('finaltrade.user_id', $user_id)
    ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
    ->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')"))
    ->get();