我试图在laravel中获取以下mysql查询。

SELECT * FROM (SELECT `tb`.owner_id,`tb`.property_id, `tb`.tenant_id FROM
`tbl_booking` tb join`tbl_ticket` tt WHERE `tb`.tenant_id = `tt`.tenant_id and
request_status = '4') AS T where T.tenant_id = 25 limit 1;


我已经能够为内部查询编写laravel等效项,如下所示:

DB::table('tbl_booking as tb')
    ->join('tbl_ticket as tt ', 'tb.tenant_id', '=', 'tt.tenant_id' )
    ->where('tb.request_status', '=', '4')
    ->get(['tb.owner_id', 'tb.property_id', 'tb.tenant_id']);


有人可以提供有关如何进行的建议吗?
PS:我正在使用Laravel 5.2

最佳答案

要仅选择一个原始用途take(1)

DB::table('tbl_booking as tb')
    ->join('tbl_ticket as tt', function($join) {
                $join->on('tb.tenant_id', '=', 'tt.tenant_id')
                     ->where('request_status',  DB::Raw(4));
                            })
    ->where('tb.tenant_id', '=', '25')
    ->take(1)
    ->get(['tb.owner_id', 'tb.property_id', 'tb.tenant_id']);

关于php - laravel查询mysql等效项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48964306/

10-16 22:42