我正在努力使用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
请帮助..
答案 0 :(得分: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中有一个pull,但最终没有实现。无论如何,您都可以在Builder.php
中查看使用查询构建器的所有选项。
当您无法使用Query Builder或Eloquent ORM执行复杂查询时,也可以使用DB:statement
。