Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用case查询Laravel数据库_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 用case查询Laravel数据库

Php 用case查询Laravel数据库,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我正在当前项目中使用Laravel5。以下是我对数据库的查询: $users = User::where('status', '=', '3')->orWhere('status', '=', '2')->whereExists(function($query1) { $query1->select(DB::raw(1)) ->from('firsts') ->wh

我正在当前项目中使用Laravel5。以下是我对数据库的查询:

$users = User::where('status', '=', '3')->orWhere('status', '=', '2')->whereExists(function($query1)
        {
             $query1->select(DB::raw(1))
               ->from('firsts')
               ->whereRaw('firsts.user_id = users.id')
               ->where('status', '=', 'approved');
        })
        ->whereExists(function($query2)
        {
             $query2->select(DB::raw(1))
               ->from('seconds')
               ->whereRaw('seconds.user_id = users.id')
               ->where('status', '=', 'approved');
        })
        ->whereExists(function($query3)
        {
             $query3->select(DB::raw(1))
               ->from('thirds')
               ->whereRaw('thirds.user_id = users.id')
               ->where('status', '=', 'approved');
        })
        ->whereExists(function($query4) use($category_id)
        {
             $query4->select(DB::raw(1))
               ->from('jobcategories')
               ->where('provider_id', '!=', 0)
               ->where('category_id', '=',$category_id);
        })
    ->get();
其背后的想法是选择所有有效用户。正如您在查询开始时看到的,我为用户编写了第一个条件。问题是,状态为“2”的用户将永远不会有第三个表状态为“已批准”。是否有可能在
$query3
之前添加if语句?提前谢谢

尝试下面的查询

$users = User::whereIn('users.status', array(2, 3))
->leftJoin('firsts f', 'f.user_id', '=', 'users.id')
->leftJoin('seconds s', 's.user_id', '=', 'users.id')
->leftJoin('thirds t', 't.user_id', '=', 'users.id')
->where(function($query) {
  $query->where('users.status', 2);
  $query->where('f.status', 'approved');
  $query->where('s.status', 'approved');
})
->orWhere(function($query) {
  $query->where('users.status', 3);
  $query->where('f.status', 'approved');
  $query->where('s.status', 'approved');
  $query->where('t.status', 'approved');
});
我不确定如何处理查询的最后一部分,因为它与任何用户(第一、第二、第三)都没有关系:

->whereExists(function($query4) use($category_id)
  {
     $query4->select(DB::raw(1))
       ->from('jobcategories')
       ->where('provider_id', '!=', 0)
       ->where('category_id', '=',$category_id);
  })

查看生成的查询。您将看到多个次优的子选择查询。我不确定您希望通过这个查询实现什么,但我猜也可以通过几个连接来实现,这些连接应该会提供更好的性能。如果您详细说明一下这个查询的目的,我可以帮助您处理连接。好的,我将尝试更详细地解释这个查询背后的逻辑。我必须选择状态为2或3的用户,其中用户在表上有记录:第一秒和第三秒,其中记录状态为已批准。问题是,状态为2的用户永远不会有状态为“已批准”的表记录。所以,如果状态为2的用户没有已批准的记录,而您希望用户有已批准的记录,为什么要选择该用户?您能给您的查询添加一些括号吗?:)是否为“在(2,3)中的状态和用户在所有表中都有已批准的记录”?或“其中状态=2或(状态=3且用户在所有表中都有已批准的记录)”?或其他?状态为2的用户必须在表1和表2上获得状态批准,但状态为3的用户必须在表1、表2和表3上获得状态批准。目前,我认为我可以使用一个选项。让状态为2的所有用户在表1和表2上检查其状态,将其id写入数组并之后,找到状态为3的所有用户,并在表firsts、seconds和Trithers中检查他们的状态,然后更新id的数组。但我认为这是错误的解决方案。用户在firsts、seconds和Trithers中最多只有一条记录还是多条记录?如果用户状态='2'$query query开始,如果用户状态='3,我是否理解正确“$builder查询开始了吗?”?