【问题标题】:Laravel Query builder query equivalent to SQL query?Laravel Query builder查询相当于SQL查询?
【发布时间】:2019-04-19 15:04:40
【问题描述】:

我的查询运行良好,唯一的问题是这个 where 子句。请告诉我什么会在 Laravel 中查询 builder 查询,相当于这个查询

WHERE (interest_request.sender_id = 6 or interest_request.to_user_id = 6) 
AND interest_request.interest_status =2 
and profiles.profile_id not in (6)

我正在使用以下查询生成器,但无法正常工作

where('to_user_id',$my_profile_id)->orwhere
('sender_id',$my_profile_id)->where('interest_status','2')
->whereNotIn('profiles.profile_id', $my_profile_id) 

【问题讨论】:

  • 请只发布部分查询。

标签: mysql laravel laravel-5 query-builder laravel-query-builder


【解决方案1】:

在您的特定情况下,您必须使用嵌套的 where 子句:

->where(function ($query) use ($my_profile_id) {
    $query->where('to_user_id', $my_profile_id)
        ->orWhere('sender_id', $my_profile_id);
})
->where('interest_status', 2)
->where('profile_id', '!=', $my_profile_id)

此外,如果列表中只有一个 id,则无需使用 whereIn(或 whereNotIn)查询。

【讨论】:

    【解决方案2】:

    我会假设它在 sql 中如下所示:

    WHERE to_user_id = $my_profile_id  
    OR sender_id = $my_profile_id
    AND interest_status = 2
    AND profiles.profile_id NOT IN ($my_profile_id)
    

    当我使用 orwhere() 语句时,我使用回调函数:

    where(function($query) {$query->something})
    ->orwhere(function($query) {$query->otherOptions})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2015-10-29
      • 2019-10-31
      • 2021-02-04
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多