Laravel Query Builder - 如何为子查询添加别名?

时间:2015-02-17 17:48:07

标签: laravel query-builder

如果我有以下代码:

DB::table('A')->join(DB::table('B'), ...)

导致这个sql:

select * from "A" inner join (select * from "B") on ...

我该如何为子查询添加别名?与select * from "A" inner join (select * from "B") as "Boron" on ...

中一样

我宁愿不必使用原始sql。

1 个答案:

答案 0 :(得分:0)

子查询的简单解决方案是 DB :: raw ,它也可以解决别名。

        $items = DB::table('A as Amy')
            ->select('*')
            ->join(
                DB::raw("(select * from B) as Boron"),
                'Amy.keyA', '=', 'Boron.keyB')
            ->get();