如何在laravel查询(元素和原始查询)中使用IN运算符? [英] how to use IN operator in laravel query (eleqouent and raw query)?
本文介绍了如何在laravel查询(元素和原始查询)中使用IN运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用IN
运算符从数据库中获取数据.我尝试如下使用它,但出现错误:
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)');
我是Laravel的新手,不完全了解如何使用IN
之类的运算符,所以请向我解释如何使用它.
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.
推荐答案
,您可以像这样在DB::raw()
中设置自定义选择:
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)'));
,或者您可以像这样使用whereIn()
:
or you can use whereIn()
like this:
DB::table('prstuff')
->select('*')
->whereIn('pid', function($query)
{
$query->select('pid')
->from('prdrop');
})
->get();
这篇关于如何在laravel查询(元素和原始查询)中使用IN运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文