1

我正在努力使用 Laravel Query Builder 构建以下嵌套的复杂查询。我正在使用 laravel 4.0。嵌套连接的主要问题。

SELECT clubs.id, clubs.slug, clubs.name as `club_name`, clubs.state, clubs.category, clubs.live, `m`.`total_members`, `m`.`online_members`, `m`.`offline_members`, `m`.`last_7days_members` as `total_members_last_7days`

            FROM clubs

            LEFT JOIN 
                (
                    SELECT id, club_id, count(*) as `total_members`, SUM(online_member = 1) AS online_member 

                    FROM members 

                    LEFT JOIN

                        (
                            SELECT posts.id, posts.member_id, posts.title, posts.body 
                            FROM posts 
                            WHERE posts.transaction_id IS NOT NULL

                        ) p on p.member_id = members.id
                    WHERE (`post_verfied` is null or `post_verfied` = 1) 
                        AND `members`.`deleted_at` is null
                    group by club_id
                ) m on m.club_id = clubs.id

            LEFT JOIN comments on comments.member_id = m.id

            WHERE clubs.`deleted_at` is null
            group by `clubs`.name

请帮忙..

4

1 回答 1

1

您可以通过DB:raw以下方式解决此问题:

DB:table('table1')->leftJoin(DB::raw('(select * 
                             from table2 
                             join table3 on table3.id = table2.id) as mytable'),
function($join)
{
    $join->on('table1.id', '=', 'mytable.id');
});

我知道这不是您真正想要的,但它是部分解决方案。调查我发现 Laravel 4.x 不支持。一年前在 Laravel 3.x 中有一个拉动,但最终没有实现。无论如何,您都可以看到您可以使用查询生成器应用的所有选项Builder.php

DB:statement当您无法使用 Query Builder 或 Eloquent ORM 执行复杂查询时,您也可以使用。

于 2013-09-23T07:44:28.683 回答