【问题标题】:How can I select data rows by id in a database query?如何在数据库查询中按 id 选择数据行?
【发布时间】:2022-01-21 02:29:27
【问题描述】:

我有一个需要进一步过滤的数据库查询。 id 需要匹配我拥有的数组中的数字。出于某种原因,我可以使用除 id 之外的每一列来选择 where。即使像$query->where('users.id', 1); 这样简单的东西也不起作用。如何通过 id 执行数据库查询?

$portfolio_query_array = UserPortfolio::all()->pluck('user_id')

$query = DB::table('users');
        $query->select('users.id as user_id','users.user_slug', 'users.social_id', 'users.social_img_update_status', 'users.avatar', 'users.first_name', 'users.last_name', 'users.bio', 'comments.rating', 'user_portfolios.title', 'user_portfolios.description', 'user_portfolios.filepath');

        $query->leftjoin('comments', 'comments.comment_to', '=', 'users.id');
        $query->leftjoin('user_portfolios', 'user_portfolios.user_id', '=', 'users.id');

        $query->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', '=', 'users.id');

        $query->where('users.status', 1);
        $query->where('users.user_type', 3);

        // The below simple query line doesn't work
        $query->where('users.id', 1);

        // The lines I do want to work also fail.
        foreach ($portfolio_query_array as $user_id){
            $query = $query + $query->where('users.id', $user_id);
        }

感谢您的帮助!

【问题讨论】:

  • 那么你需要从除id之外的所有列中搜索数据吗?
  • 您应该查看whereIn 方法,您可以将user_ids 列表传递给... 旁注:您可以在构建器UserPortfolio::pluck('user_id') 上调用pluck

标签: php mysql laravel database


【解决方案1】:

首先,您应该利用 Eloquent ORM,除非您遇到一些性能问题或非常复杂的查询,否则无需使用原始查询。 假设您有 3 个模型 UserUserPortfolioComment,并定义了所有关系,您可以执行以下简单操作:

$users = User::with('comments', 'portfolio')
   ->where('status', 1)
   ->where('user_type', 3)
   ->whereHas('portfolio')
   ->get();

通过这种方式,您可以让所有用户拥有我认为您想要的投资组合。

无论如何,如果您坚持使用原始查询,那么whereIn() 方法就是您所需要的

$portfolio_query_array = UserPortfolio::pluck('user_id')->toArray();

$query = DB::table('users')
    ->select(
         'users.id as user_id',
         'users.user_slug', 
         'users.social_id', 
         'users.social_img_update_status', 
         'users.avatar', 
         'users.first_name', 
         'users.last_name', 
         'users.bio', 
         'comments.rating', 
         'user_portfolios.title', 
         'user_portfolios.description', 
         'user_portfolios.filepath'
     )
     ->leftjoin('comments', 'comments.comment_to', 'users.id')
     ->leftjoin('user_portfolios', 'user_portfolios.user_id', 'users.id')
     ->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', 'users.id')
     ->where('users.status', 1)
     ->where('users.user_type', 3)
     ->whereIn('users.id', $portfolio_query_array)
     ->get();

顺便说一句,如果没有从该表中选择任何字段,您为什么要对 user_profile_category_selects 执行 leftJoin

此外,如果您要从整个投资组合列表开始寻找所有拥有投资组合的用户,那么加入投资组合并只获取那些拥有投资组合的用户不是更好吗?

$query = DB::table('users')
    ->select(
         'users.id as user_id',
         'users.user_slug', 
         'users.social_id', 
         'users.social_img_update_status', 
         'users.avatar', 
         'users.first_name', 
         'users.last_name', 
         'users.bio', 
         'comments.rating', 
         'user_portfolios.title', 
         'user_portfolios.description', 
         'user_portfolios.filepath'
     )
     ->leftjoin('comments', 'comments.comment_to', 'users.id')
     ->join('user_portfolios', 'user_portfolios.user_id', 'users.id')
     ->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', 'users.id')
     ->where('users.status', 1)
     ->where('users.user_type', 3)
     ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多