如何在Laravel 5.1中编写此代码(左联接,子查询)? [英] How to write this (left join, subquery ) in Laravel 5.1?

查看:257
本文介绍了如何在Laravel 5.1中编写此代码(左联接,子查询)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Laravel 5.1中编写此查询:

How to write this query in Laravel 5.1:

SELECT p.id, p.title, p.created_at, p.updated_at, u.name, COALESCE(c.comments_count, 0) AS comments_count, COALESCE(pl.status_sum, 0) AS status_sum

FROM posts p

LEFT OUTER JOIN users u ON u.id = p.user_id

LEFT OUTER JOIN (
    SELECT pl.post_id, SUM(pl.status) AS status_sum
    FROM postslikes pl
    GROUP BY pl.post_id
) pl ON pl.post_id = p.id

LEFT OUTER JOIN (
    SELECT c.post_id, COUNT(*) as comments_count
    FROM comments c
    GROUP BY c.post_id
) c ON c.post_id = p.id ORDER BY comments_count DESC

我需要它进行分页.我可以毫无问题地执行此查询,但是手动分页器始终提供相同的结果: http://laravel.com/docs/5.1/pagination#manually-creating -a-paginator 与此处相同的问题: 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

我的尝试没有成功:

DB::table( 'posts' )
    ->select( 'posts.id', 'posts.title', 'posts.created_at', 'posts.updated_at', 'users.name', DB::raw( 'COALESCE( comments.body, 0 ), COALESCE( postslikes.status, 0 )' ) )

    ->leftJoin( 'users', 'users.id', '=', 'posts.user_id' )

    ->leftJoin( DB::raw( 'SELECT postslikes.post_id, SUM( postslikes.status ) FROM postslikes GROUP BY postslikes.post_id' ), function( $join )
    {
        $join->on( 'postslikes.post_id', '=', 'post.id' );
    })

    ->leftJoin( DB::raw( 'SELECT comments.post_id, COUNT(*) FROM comments GROUP BY comments.post_id' ), function( $join )
    {
        $join->on( 'comments.post_id', '=', 'post.id' );
    })

    ->get();

我认为问题是comment_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:

->leftJoin(DB::raw("(SELECT [...]) AS p"), 'p.post_id', '=', 'posts.id')

最好像在原始查询中一样为计算字段创建别名:

It's also better to create an alias for calculated fields, as you did in your raw query:

COUNT(*) AS count

尽管进行了这些更改,除非我错了,否则可以从简化查询开始.通过这种方式删除子查询:

Despite this changes, unless I'm wrong, you can start by making your query simpler. Drop the subqueries, this way:

SELECT
  p.id,
  p.title,
  p.created_at,
  p.updated_at,
  u.name,
  COUNT(c.id) AS comments_count,
  COALESCE(SUM(pl.status), 0) AS status_sum
FROM
  posts p
LEFT OUTER JOIN
  users u
ON 
  u.id = p.user_id
LEFT OUTER JOIN 
  postslikes pl
ON 
  pl.post_id = p.id
LEFT OUTER JOIN 
  comments c
ON 
  c.post_id = p.id 
ORDER BY 
  comments_count DESC
GROUP BY
  p.id

然后,通过这个新查询,您可以使用Laravel进行构建:

Then, with this new query, you can use Laravel to build it:

DB::table('posts')
  ->select([
    'posts.id',
    'posts.title',
    'posts.created_at',
    'posts.updated_at',
    'users.name',
    DB::raw('COUNT(comments.id) AS comments_count'),
    DB::raw('COALESCE(SUM(postslikes.status), 0) AS status_sum'),
  ])
  ->leftJoin('users', 'users.id', '=', 'posts.user_id')
  ->leftJoin('comments', 'comments.post_id', '=', 'posts.id')
  ->leftJoin('postslikes', 'postslikes.post_id', '=', 'posts.id')
  ->orderBy('comments_count', 'DESC')
  ->groupBy('posts.id')
  ->get();

请注意,我假设您在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中编写此代码(左联接,子查询)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆