简体   繁体   English

Laravel在语句中选择多个where

[英]Laravel select with multiple where in statement

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. 如果我只采用一个where子句,它会完美地工作。 Example: 例:

$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. 我什至尝试用另一列而不是标题,但是第二个where函数不起作用。 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. 我想检索标题表中标题为“城市和城堡”的行中的行,我之前在单个select语句中使用了多个where子句,并且它起作用了。 Not now. 现在不要。 Any suggestions? 有什么建议么? Thanks in advance. 提前致谢。

You said: 你说:

I want to retreive the rows from titles table where the title is 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_id then you may use it like: 如果您想为titles.char_id添加另一个where子句,则可以像这样使用它:

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

You may chain as much where as you need before you call get() method. 您可能链尽可能多的where ,因为你需要在打电话之前, get()方法。 You can add the where('char_id', 5) after the whereIn like whereIn(...)->where('char_id', 5) and then call get() . 您可以添加在where('char_id', 5)whereInwhereIn(...)->where('char_id', 5)然后调用get()

If you have a Title model 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: 与使用DB相同,仅将DB::table('titles')替换为Title ,例如:

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

What about Character here ? Character呢?

I don't really know how work your double ->where( in php, but in sql here is the mistake : 我真的不知道您的double- ->where(在php中如何工作,但是在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中执行此操作的方法。

Assuming you are using Laravel 4 假设您正在使用Laravel 4

And Character is your model extended from Eloquent 而性格是您从口才扩展的模型

don't mix FIND and WHERE. 不要将FIND和WHERE混在一起。

Find is for single usage find AND sorting afterward (so order by, and etc) 查找是针对单次使用的查找,然后再进行排序(因此,排序依据等)

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)

this way you respect eloquent's features. 这样,您尊重雄辩者的特征。

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表示法...所以选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM