如何在 Laravel 5.1 中编写这个(左连接、子查询)?
问题描述
如何在 Laravel 5.1 中编写此查询:
How to write this query in Laravel 5.1:
我需要它来进行分页.我可以毫无问题地执行这个查询,但是手动分页器总是给出相同的结果:http://laravel.com/docs/5.1/pagination#manually-creating-a-分页器与此处相同的问题:http://laravel.io/forum/07-22-2015-laravel-51-manual-pagination-not-working-as-expected
I need it for Pagination. I can perform this query raw without any problems but the manually paginator gives always the same results: http://laravel.com/docs/5.1/pagination#manually-creating-a-paginator The same problem as here: http://laravel.io/forum/07-22-2015-laravel-51-manual-pagination-not-working-as-expected
我的尝试没有成功:
我认为问题在于comments_count 和status_sum?谢谢!
I think the problem is comments_count and status_sum? Thanks!
推荐答案
要在 Laravel 的查询构建器中使用子查询,您应该将其添加到连接中,如下所示:
To use subqueries with Laravel's query builder, you should add it to the join as follows:
最好为计算字段创建别名,就像您在原始查询中所做的那样:
It's also better to create an alias for calculated fields, as you did in your raw query:
尽管发生了这些变化,除非我错了,否则您可以从简化查询开始.以这种方式删除子查询:
Despite this changes, unless I'm wrong, you can start by making your query simpler. Drop the subqueries, this way:
然后,通过这个新查询,您可以使用 Laravel 来构建它:
Then, with this new query, you can use Laravel to build it:
请注意,我假设您的 comments
表中有一个名为 id
的列作为主键.
Note that I'm assuming you have a column named id
in your comments
table that is the primary key.
这篇关于如何在 Laravel 5.1 中编写这个(左连接、子查询)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!