2016-07-28 42 views
1

我想拉一羣用戶自指定日期以來沒有進行交易。Laravel中的嵌套查詢5.2查詢生成器拉MAX日期

這是我的查詢:

$notActive = DB::table('transactions') 
    ->join('users','transaction.user_id', '=', 'users.user_id') 
    ->select(DB::raw('users.user_id, users.name, max(transaction.last_posted_date) AS lastDate')) 
    ->groupBy('users.user_id') 
    ->where('lastDate', '<', ''.$not.''); 

    return Datatables::of($notActive) 
    ->make(true); 

$不能是日期值拉到這將產生一個Ajax數據表中的錯誤。我已經嘗試了這種多種方式,包括在where子句中將最大日期的select語句放入,但不斷拋出錯誤。有沒有更好的方法來查詢我想要的這些數據?謝謝。

回答

1

我想通了!使用having子句提出了交易日期小於輸入字段提供日期的交易的用戶(即,不活躍自從)

$notActive = DB::table('transactions') 
    ->join('users','transactions.user_id', '=', 'users.user_id') 
    ->select(DB::raw('users.user_id, users._name, max(transactions.last_posted_date) as last_posted_date')) 
    ->groupBy('users.user_id') 
    ->having('transactions.last_posted_date', '<', ''.$not.')'); 

    return Datatables::of($latestTransactions) 
    ->make(true);