简体   繁体   中英

Left join 2 times raw query laravel doesn't work (Laravel)

I tried my raw query sql in laravel and i need to left join it 2 times but the print out is wrong. It different from mysql. I've tried in mysql and it works well

this is my code :

$tabel = DB::SELECT(DB::RAW("
            SELECT codeh.info_code kh_infocode, codep.no_code khp_nocode, 
            codep.info_code khp_infocode, 
            codep.name_code khp_namecode, t.* FROM transactions t 
            LEFT JOIN users u ON t.user_id=u.id
            LEFT JOIN divisions d ON d.id=u.division_id
            LEFT JOIN all_codes codep ON t.code_p_id=codep.id
            LEFT JOIN all_codes codeh ON t.allcode_id=codeh.id
            WHERE 1=1
            AND d.id=$div_id
            AND codep.no_code LIKE '$allcode->no_code%'
            $db_date
            ORDER BY t.date, khp_nocode
            "));

it works really well in sql but when i put it in laravel kh_infocode change and the value same like khp_infocode even though kh_infocode has different value with khp_infocode

Everything is in the official docs. It's not even long, go give it a read.

DB::table('transactions as t')
->select(
    'codeh.info_code',
    'kh_infocode',
    'codep.no_code',
    'khp_nocode',
    'codep.info_code',
    'khp_infocode',
    'codep.name_code',
    'khp_namecode',
    't.*',
)
->leftJoin('users as u', 't.user_id', '=', 'u.id')
->leftJoin('all_codes as codep', 't.code_p_id', '=', 'codep.id')
->leftJoin('all_codes as codeh', 't.allcode_id', '=', 'codeh.id')
->where('d.id', '=', $div_id)
->where('codep.no_code', 'like', $allcode->no_code.'%')
->orderBy('t.date')
->orderBy('khp_nocode')
->get();

只需直接设置SQL字符串即可,无需DB :: RAW()

$tabel = DB::SELECT('your select statment');

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