如何在Laravel查询中使用AND运算符?

时间:2019-02-16 22:52:25

标签: php mysql laravel

选择一个用户

where id =! Auth::user->id 

// this prevents the query returning the current user logged in 

where Auth::user->value1 ,'=', 'value1'  ||   'value2' || 'value3' 

因此,用英语查找一个用户名,其中id不等于当前登录的用户id,并且已验证用户的value1列等于另一个用户的value1或value2或value 3column

下面是一条语句,我已经完成了,但是无法正常运行,但是没有OR运算符

where(Auth::user()->recommendationWord1, '=' , 'value1') 

此语句是格式的示例,包含我尝试过的内容。

$Query = DB::table('users')
    ->select('username')
    ->where('id', '!=', Auth::user()->id)
    ->whereIn(Auth::user()->recommendationWord1 , '=', 'value1', '||' , 'value2', '||' , 'value3'
    ->get();

下面是我的HTML页面的代码以及我的发布方式

@foreach ($Query as $selectedUser)

    Code formatting<p> {{ $selectedUser->username}}</p>  

@endforeach  

3 个答案:

答案 0 :(得分:2)

您是否尝试过分组参数?

$Query = DB::table('users')
        ->select('username')
        ->where('id', '!=', Auth::user()->id)
        ->where(function($query){
              $query->where(Auth::user()->recommendationWord1, '=', 'value1')
              ->orWhere(Auth::user()->recommendationWord1, '=', 'value2')
              ->orWhere(Auth::user()->recommendationWord1, '=', 'value3');
              })
         ->get();

请参阅文档中的示例:

有时,您可能需要创建更高级的where子句,例如“ where where”子句或嵌套参数分组。 Laravel查询构建器也可以处理这些。首先,让我们看一下括号内分组约束的示例:

DB::table('users')
            ->where('name', '=', 'John')
            ->where(function ($query) {
                $query->where('votes', '>', 100)
                      ->orWhere('title', '=', 'Admin');
            })
            ->get();
enter code here

如您所见,将Closure传递给where方法将指示查询构建器开始约束组。闭包将收到一个查询构建器实例,您可以使用它来设置应包含在括号组中的约束。上面的示例将产生以下SQL:

select * from users where name = 'John' and (votes > 100 or title = 'Admin')

https://laravel.com/docs/5.7/queries#where-clauses

答案 1 :(得分:2)

尝试一下:

$users = User
            ::where('id', '<>', auth()->id())
            ->whereIn(auth()->user()->recommendationWord1, ['value1', 'value2', 'value3']) 
            ->get()

答案 2 :(得分:1)

结构中的Select A as EngineName, totalInvoice, fcQuantity, maintain From ( (select e.ename as 'A', ISNUll(SUM(fc.quantity),0) as fcQuantity, ISNUll(SUM(m.pricetotal),0) as maintain FROM Engine e left Join FuelConsumption fc on fc.engineid = e.id left Join Maintenance m on m.engineid = e.id Group By e.ename) t1 left Join (select e.ename as 'B', ISNUll(SUM(ch.total),0) as totalInvoice FROM Engine e Left Join ElectricBox eb On eb.engineid = e.id Left join ECounter ec on ec.boxid = eb.ID Left join Registration r on r.counterid=ec.ID Left join CounterHistory ch on ch.regid = r.id Group By e.ename) t2 On t1.A = t2.B) 就您而言

->whereIn('attribute', ['val1', 'val2', ...])