0

我是 laravel 新手,我想在 laravel 5.4 中编写 mysql 查询。查询就像:

注意:避免列名..

SELECT * 
FROM (SELECT DISTINCT * 
      FROM messages 
      WHERE (from_id=1 OR to_id=1) 
ORDER BY created DESC) as m 
GROUP BY `from_id` 

我试过但给出错误。

$messages = DB::table('messagetbl')
                ->select('*')
                ->Where(function($query) use ($userid){
                    $query->distinct()
                    ->where('senderid',$userid)
                    ->orWhere('receiverid',$userid)
                    ->orderBy('datetime','desc');
                 })
                ->groupBy('senderid')
                ->get();

错误:

SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列“db.messagetbl.id”;这与 sql_mode=only_full_group_by 不兼容(SQL:select * from messagetblwhere ( senderid= 8 or receiverid= 8) group by senderid

提前致谢。

4

1 回答 1

0

我相信这样的事情:

  DB::table('messages')
        ->select('*') 
        ->where('form_id', '=', 1)
        ->orWhere('to_id', '=', 1)
        ->orderBy('created', 'desc')
        ->groupBy('form_id')
        ->distinct()
        ->get();
于 2017-06-25T16:45:32.303 回答