how to use IN operator in laravel query (eleqouent and raw query)?

浪尽此生 提交于 2020-02-07 17:16:34

问题


I need to use the IN operator to get the data from the database. I tried using it as below and got an error:

$pr =DB::('select * from prstuff p where p.pid in (select pid from prdrop)'); 

I am new to Laravel and don't know exactly how to use the operators like IN, so please explain to me how to use it.


回答1:


you can set the custom select in DB::raw() like this :

DB::select(DB::raw('select * from prstuff p where p.pid in (select pid from prdrop)'));

or you can use whereIn() like this:

DB::table('prstuff')
->select('*')
->whereIn('pid', function($query)
{
    $query->select('pid')
    ->from('prdrop');
})
->get();



回答2:


You are not calling any funtion on your db class. You can call the select function like this DB::select ()

$pr =DB::select('select * from prstuff p where p.pid in (select pid from prdrop)');


来源:https://stackoverflow.com/questions/35724814/how-to-use-in-operator-in-laravel-query-eleqouent-and-raw-query

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