Laravel 4: add whereIn clause to a join condition

感情迁移 提交于 2019-12-11 09:37:11

问题


I want to run the below query

SELECT * FROM order_item JOIN order ON (order_item.order_id = order.id AND order.order_status IN ('payment_completed','refund_requested')) WHERE order_item.item_id=1;

I tried like below.

OrderItem::join('order', function($join){
            $join->on('order.id','=','order_item.order_id');
            $join->whereIn('order.order_status',array('payment_completed','refund_requested'));
    })->where('order_item.item_id','=','1')->get();

I know this is wrong. What is the right way to use whereIn clause in join condition?

Thanks in advance!


回答1:


Hell, I have another option for do this thing.

You can use FIND_IN_SET('ABC','ABC,CDE,DEF,XYZ');

It will return true if condition true so you can add this condition in join cause.

OrderItem::join('order', function($join){
        $join->on('order.id','=','order_item.order_id')
        $join->on(\DB::raw("FIND_IN_SET(order.order_status,'payment_completed,refund_requested')"),"=",\DB::raw('"1"'));

})->where('order_item.item_id','=','1')->get();


来源:https://stackoverflow.com/questions/26913776/laravel-4-add-wherein-clause-to-a-join-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!