我有這個查詢:
SELECT `topic_posts`.*, `users`.`id` AS `adminID`, `users`.`name` as `adminName` FROM `topic_posts` INNER JOIN `topics` ON `topics`.`id` = `topic_posts`.`topic` INNER JOIN `users` ON `users`.`id` = `topic_posts`.`player` WHERE `users`.`playerAdminLevel` > 0 AND `topics`.`type` = 0 GROUP BY (`topic_posts`.`id`)
我想在 Laravel 中使用,例如:
$result = DB::table('topic_posts`)->join..etc
而且,使用->paginate(20)
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
實作您所需要的可能性很小。
Pure select,使用您的整個 SQL 查詢:
DB::select('select topic_posts,* .....');
在此處查看更多資訊: https ://laravel.com/docs/4.2/database#running-queries
或者您也可以按照您開始構建查詢的方式進行,例如:
DB::table('topic_posts')
->join('topics', 'topics.id', '=', 'topic_posts.topic')
->join('users', 'users.id', '=', 'topic_posts.player')
->select('`topic_posts`.*', 'users.id') ...
更多資訊可以在這里找到: https ://laravel.com/docs/9.x/queries#inner-join-clause
uj5u.com熱心網友回復:
SELECT `topic_posts`.*, `users`.`id` AS `adminID`, `users`.`name` as `adminName` FROM `topic_posts` INNER JOIN `topics` ON `topics`.`id` = `topic_posts`.`topic` INNER JOIN `users` ON `users`.`id` = `topic_posts`.`player` WHERE `users`.`playerAdminLevel` > 0 AND `topics`.`type` = 0 GROUP BY (`topic_posts`.`id`)
會是這樣的(我必須對查詢進行一些更改,因為您似乎沒有正確使用 group by):
DB::table('topic_posts')
->join('topics', 'topic.id', '=', 'topic_posts.topic')
->join('users', 'users.id', '=', 'topic_posts.player')
->select('topic_posts.id', 'users.id as adminID', DB::raw('count(topic_posts.id) as totalTopicPosts'))
->where('users.playerAdminLevel', '>', 0)
->where('topics.topics', 0)
->groupBy('topic_posts.id', 'users.id')
->paginate(20)
但是如果你想使用分組依據,首先要確保你有一些聚合,比如計數或其他。在此處查看檔案:https ://www.tutorialspoint.com/sql/sql-group-by.htm
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457832.html
