相当于mysql的laravel查询

时间:2018-02-24 15:12:51

标签: php mysql laravel

我试图在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']);

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

2 个答案:

答案 0 :(得分:0)

仅选择一个原始用途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']);

答案 1 :(得分:0)

这应该有效

DB::table('tbl_booking')->select('tbl_booking.owner_id', 'tbl_booking.property_id', 'tbl_booking.tenant_id')
    ->join('tbl_ticket ', 'tbl_ticket .tenant_id', '=', 'tbl_booking.tenant_id' )
    ->where('tbl_booking.request_status', '=', '4')
    ->where('tbl_booking.tenant_id', '=', '25')        
    ->first();