【问题标题】:Write Nested Select Query in Laravel 5.3在 Laravel 5.3 中编写嵌套选择查询
【发布时间】:2017-03-25 07:12:41
【问题描述】:

如何在 Laravel 5.3 中编写复杂查询?我正在尝试,但没有得到我期望的结果。

查询

SELECT * FROM (SELECT posts.post_id 
FROM posts 
WHERE ((posts.user_id = 1 AND posts.user_type = 'user') 
    OR (posts.user_id IN (1) AND posts.user_type = 'page'))) posts 
    WHERE posts.post_id > '0' ORDER BY posts.post_id DESC

请帮助我使用 Laravel 查询生成器编写此内容。

【问题讨论】:

    标签: sql laravel laravel-5.2 laravel-query-builder


    【解决方案1】:

    @Punit Gajjar 提供了一种解决方案,但这并不是您提出的Query Builder 术语的问题。他的解决方案会起作用,但其中一半没有使用查询构建器(它只是复制/粘贴并将您的 SQL 放入原始查询中,这与您之前的基本完全相同),因此我觉得有必要为您提供额外的选择:

    Post::where('post_id', '>', 0)
        ->where(function($query) {
    
           $query->where(function($subquery) {
               $subquery->where('user_id', 1)->where('user_type', 'user');
           })->orWhere(function($subquery) {
               $subquery->whereIn('user_id', [1])->where('user_type', 'page');
           });
    
        })
        ->orderBy('post_id', 'DESC')
        ->get();
    

    出于可读性目的,我将变量名称保持简短,但匿名函数($query$subquery)中的参数是查询构建器实例。

    【讨论】:

      【解决方案2】:

      这是你的答案。

      $MyQuery = DB::table(DB::Raw("(SELECT
                                     posts.post_id
                                     FROM
                                     posts
                                     WHERE
                                     (
                                       (
                                         posts.user_id = 1
                                         AND posts.user_type = 'user'
                                        )
                                        OR 
                                        (
                                          posts.user_id IN (1)
                                          AND posts.user_type = 'page'
                                         )
                                      )
                                      )"))
                  ->where('posts.post_id','>',"0")->orderBy("posts.post_id" , "DESC")->get();
      

      尝试像这样使用->toSql() 打印一次查询

      echo $MyQuery = DB::table(DB::Raw("(SELECT
                                          posts.post_id
                                          FROM
                                          posts
                                          WHERE((
                                                 posts.user_id = 1
                                                 AND posts.user_type = 'user'
                                                 )
                                                 OR(
                                                 posts.user_id IN (1)
                                                 AND posts.user_type = 'page'
                                                 )))"))
                  ->where('posts.post_id','>',"0")->orderBy("posts.post_id" , "DESC")->toSql();
      
                  die();
      

      【讨论】:

      • 这个感谢的好答案,但我使用模型来构建 SELECT 查询
      • @NarendraJhala this 可以帮助你更多了解,
      猜你喜欢
      • 2013-02-01
      • 1970-01-01
      • 2017-11-24
      • 2021-01-01
      • 2018-05-08
      • 2013-01-19
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多