简体   繁体   中英

laravel query for mysql equivalent

I am trying to get the following mysql query in laravel.

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;

I have been able to write the laravel equivalent for the inner query, which is below:

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']);

Could someone provide suggestions on how to proceed? PS: I am using Laravel 5.2

To select only one raw use 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']);

This should works

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM