【问题标题】:how to use IN operator in laravel query (eleqouent and raw query)?如何在 laravel 查询(eleqouent 和 raw 查询)中使用 IN 运算符?
【发布时间】:2023-03-12 09:29:01
【问题描述】:

我需要使用IN 运算符从数据库中获取数据。我尝试如下使用它并收到错误:

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

我是 Laravel 新手,不知道如何使用 IN 这样的运算符,所以请解释一下如何使用它。

【问题讨论】:

    标签: php laravel laravel-5 laravel-routing laravel-5.2


    【解决方案1】:

    您可以像这样在DB::raw() 中设置自定义选择:

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

    或者你可以像这样使用whereIn()

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

    【讨论】:

      【解决方案2】:

      你没有在你的 db 类上调用任何函数。你可以像这样调用select函数DB::select ()

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

      【讨论】:

      • 其实我忘了在这里写'select' ..我用同样的方式,但没有工作。我有下面的asnwer工作
      • 当然你也可以使用 DB::raw 来包装你的查询
      猜你喜欢
      • 1970-01-01
      • 2016-04-09
      • 2020-01-02
      • 2012-01-08
      • 2012-05-21
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      相关资源
      最近更新 更多