Laravel 选择多个 where in 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23304880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:25:31  来源:igfitidea点击:

Laravel select with multiple where in statement

phpsqllaravel

提问by Serban Spirescu

I have tried various methods to resolve this issue, but none worked for me.

我尝试了各种方法来解决这个问题,但没有一个对我有用。

1st method:

第一种方法

$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle'); 
$title = $title->where('title', '=', 'City');
$title = $title->get();

2nd method:

方法二

$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->where('title', '=', 'City')->get();

3rd method:

第三种方法

$title = DB::select(DB::raw("select * from titles where titles.char_id = 5 and title = 'Castle' and title = 'City'"));

None of the above methods work. If I take only one where clause it works perfectly. Example:

以上方法均无效。如果我只使用一个 where 子句,它就可以完美地工作。例子:

$title = Character::find($selected_char->id)->title()->where('title', '=', 'City')->get();

$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->get();

I even tried to take another column than title, but it doesn't work with a second where function. I want to retreive the rows from titles table where the title is City AND Castle I have used multiple where clauses before in a single select statement and it worked. Not now. Any suggestions? Thanks in advance.

我什至尝试采用除标题之外的另一列,但它不适用于第二个 where 函数。我想从标题表中检索行,其中标题是 City AND Castle 我之前在单个 select 语句中使用了多个 where 子句并且它起作用了。现在不要。有什么建议?提前致谢。

回答by The Alpha

You said:

你说:

I want to retreive the rows from titles table where the title is City AND Castle

我想从 titles 表中检索标题为 City AND Castle 的行

You may try this:

你可以试试这个:

$rowCOllection = DB::table('titles')
                   ->whereIn('title',  array('City', 'Castle'))->get();

Using multiple where:

使用多个where

$rowCOllection = DB::table('titles')
                   ->where('title', 'City')
                   ->where('title', 'Castle')->get();

If you want to add another where clause for titles.char_idthen you may use it like:

如果你想添加另一个 where 子句,titles.char_id那么你可以像这样使用它:

 $rowCOllection = DB::table('titles')
                   ->where('title', 'City')
                   ->where('title', 'Castle')
                   ->where('char_id', 5)->get();

You may chain as much whereas you need before you call get()method. You can add the where('char_id', 5)after the whereInlike whereIn(...)->where('char_id', 5)and then call get().

where在调用get()方法之前,您可以根据需要尽可能多地链接。您可以where('char_id', 5)whereIn喜欢之后添加whereIn(...)->where('char_id', 5),然后调用get()

If you have a Titlemodel then you may do the same thing using:

如果你有一个Title模型,那么你可以使用以下方法做同样的事情:

Title::where(...)->where(...)->get();

Same as using DB, only replace the DB::table('titles')with Title, for example:

与 using 相同DB,仅替换DB::table('titles')with Title,例如:

$rowCOllection = Title::where('title', 'City')
     ->where('title', 'Castle')
     ->where('char_id', 5)->get();

What about Characterhere ?

怎么样Character吗?

回答by Ryx5

I don't really know how work your double ->where(in php, but in sql here is the mistake :

我真的不知道你->where(在 php 中的 double是如何工作的,但是在 sql 中这是错误的:

When you say where title = 'a' and title = 'b', it's like you say : ok give me something where 0=1 it returns nothing.

当你说 时where title = 'a' and title = 'b',就像你说:好的,给我一些 0=1它什么都不返回的东西

You can do :

你可以做 :

select * from titles where titles.char_id = 5 and (title = 'Castle' or title = 'City')

Retrieve all data where title equals castle or city

检索标题等于城堡或城市的所有数据

Or

或者

select * from titles where titles.char_id = 5 and title IN ('Castle','City')

Retrieve all data where title equals castle or city using IN

使用 IN 检索标题等于城堡或城市的所有数据

I'm pretty sure you will find a way to do that in PHP too.

我很确定你也会在 PHP 中找到一种方法来做到这一点。

回答by azngunit81

Assuming you are using Laravel 4

假设您使用的是 Laravel 4

And Character is your model extended from Eloquent

而 Character 是你从 Eloquent 扩展而来的模型

don't mix FIND and WHERE.

不要混用 FIND 和 WHERE。

Find is for single usage find AND sorting afterward (so order by, and etc)

Find 用于单次使用 find AND 之后排序(所以 order by 等等)

So if you want to chain up your query

所以如果你想链接你的查询

Character::where()->where()->where()-get() (don't forget the get or else you wont get a result)

Character::where()->where()->where()-get() (don't forget the get or else you wont get a result)

this way you respect eloquent's features.

这样你就尊重了 eloquent 的特点。

Note your first method with ->title()is flawed because your calling a function that you custom created inside your model - thats why it wouldn't have worked.

请注意,您的第一种方法有->title()缺陷,因为您调用了在模型中自定义创建的函数 - 这就是它不起作用的原因。

Note: WereWolf Alpha's method will also work IF you don't want to use Eloquent because the code that he presented will work but thats Fluent notation...so take your pick.

注意:如果您不想使用 Eloquent,WereWolf Alpha 的方法也可以使用,因为他提供的代码可以使用,但那是 Fluent 表示法……所以请选择。