我有以下代码:
$get_order = DB::table('orders')
->leftJoin('postcodes', 'orders.postcode', '=', 'postcodes.postcode')
->where('order_ref', '=', $order_ref)
->first();
我想要实现的是,如果postcodes.postcode
以orders.postcode
开头,则执行左连接。有没有办法做到这一点?
提前致谢。
答案 0 :(得分:1)
$get_order = DB::table('orders')
->leftJoin('postcodes', 'postcodes.postcode','LIKE', DB::raw( "CONCAT(orders.postcode, '%')" ))
->where('order_ref', '=', $order_ref)
->first();
答案 1 :(得分:0)
试一下
->leftJoin('postcodes', function($join) {
$join->on('postcodes.postcode', '=', 'orders.postcode')
->on('postcodes.postcode', 'LIKE', "startWith%"));
})