问题
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