Php 使用多个where in语句的Laravel select

Php 使用多个where in语句的Laravel select,php,sql,laravel,Php,Sql,Laravel,我尝试过各种方法来解决这个问题,但没有一种对我有效 第一种方法: $title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle'); $title = $title->where('title', '=', 'City'); $title = $title->get(); $title = Character::find($selected_char->

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

第一种方法

$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle'); 
$title = $title->where('title', '=', 'City');
$title = $title->get();
$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->where('title', '=', 'City')->get();
$title = DB::select(DB::raw("select * from titles where titles.char_id = 5 and title = 'Castle' and title = 'City'"));
第二种方法

$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle'); 
$title = $title->where('title', '=', 'City');
$title = $title->get();
$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->where('title', '=', 'City')->get();
$title = DB::select(DB::raw("select * from titles where titles.char_id = 5 and title = 'Castle' and title = 'City'"));
第三种方法

$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle'); 
$title = $title->where('title', '=', 'City');
$title = $title->get();
$title = Character::find($selected_char->id)->title()->where('title', '=', 'Castle')->where('title', '=', 'City')->get();
$title = DB::select(DB::raw("select * from titles where titles.char_id = 5 and title = 'Castle' and title = 'City'"));
上述方法都不起作用。如果我只取一个where子句,它就可以完美地工作。例如:

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

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

我甚至试着用另一个专栏来代替标题,但它不能使用第二个where函数。我想检索标题为City和Castle的titles表中的行。我以前在一个select语句中使用了多个where子句,它起到了作用。不是现在。有什么建议吗?提前谢谢。

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

当你说
其中title='a'和title='b'
时,就像你说:好吧,给我一些0=1它什么也不返回的东西

你可以做:

select * from titles where titles.char_id = 5 and (title = 'Castle' or title = 'City')
检索标题等于城堡或城市的所有数据

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


我很肯定你也会在PHP中找到这样做的方法。

我真的不知道你的double
->where(
在PHP中,但在sql中,这里是一个错误:

当你说
其中title='a'和title='b'
时,就像你说:好吧,给我一些0=1它什么也不返回的东西

你可以做:

select * from titles where titles.char_id = 5 and (title = 'Castle' or title = 'City')
检索标题等于城堡或城市的所有数据

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

我很肯定你也会在PHP中找到这样做的方法。

你说过:

我想从标题为城市和城堡的标题表中检索行

你可以试试这个:

$rowCOllection = DB::table('titles')
                   ->whereIn('title',  array('City', 'Castle'))->get();
使用多个
,其中

$rowCOllection = DB::table('titles')
                   ->where('title', 'City')
                   ->where('title', 'Castle')->get();
如果要为
titles.char\u id
添加另一个where子句,则可以如下使用:

 $rowCOllection = DB::table('titles')
                   ->where('title', 'City')
                   ->where('title', 'Castle')
                   ->where('char_id', 5)->get();
在调用
get()
方法之前,您可以根据需要将
where
链接到任何位置。您可以在
的where('char\u id',5)
之后添加
where('char\u id',5)
,然后调用
get()

如果您有
标题
型号,则可以使用以下方法执行相同操作:

Title::where(...)->where(...)->get();
与使用
DB
相同,仅将
DB::table('titles')
替换为
Title
,例如:

$rowCOllection = Title::where('title', 'City')
     ->where('title', 'Castle')
     ->where('char_id', 5)->get();
这里的
字符如何?

你说:

我想从标题为城市和城堡的标题表中检索行

你可以试试这个:

$rowCOllection = DB::table('titles')
                   ->whereIn('title',  array('City', 'Castle'))->get();
使用多个
,其中

$rowCOllection = DB::table('titles')
                   ->where('title', 'City')
                   ->where('title', 'Castle')->get();
如果要为
titles.char\u id
添加另一个where子句,则可以如下使用:

 $rowCOllection = DB::table('titles')
                   ->where('title', 'City')
                   ->where('title', 'Castle')
                   ->where('char_id', 5)->get();
在调用
get()
方法之前,您可以根据需要将
where
链接到任何位置。您可以在
的where('char\u id',5)
之后添加
where('char\u id',5)
,然后调用
get()

如果您有
标题
型号,则可以使用以下方法执行相同操作:

Title::where(...)->where(...)->get();
与使用
DB
相同,仅将
DB::table('titles')
替换为
Title
,例如:

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

这里的
字符如何?

假设您使用的是Laravel 4

角色是你从雄辩延伸而来的模式

不要混淆查找和查找位置

Find用于单次使用后的查找和排序(so order by等)

所以如果你想把你的查询链起来

Character::where()->where()->where()-get()

这样你就尊重了雄辩者的特征

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


注意:如果你不想使用雄辩,狼人阿尔法的方法也会起作用,因为他给出的代码会起作用,但这是流畅的符号……所以你可以选择。

假设你使用的是拉威尔4

角色是你从雄辩延伸而来的模式

不要混淆查找和查找位置

Find用于单次使用后的查找和排序(so order by等)

所以如果你想把你的查询链起来

Character::where()->where()->where()-get()

这样你就尊重了雄辩者的特征

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


注意:如果你不想使用雄辩,狼人阿尔法的方法也会起作用,因为他提供的代码会起作用,但这是流畅的符号……所以请选择。

$title=Character::find($selected\u char->id)->title()->其中('title',array('City',Castle')->get();对我有用。谢谢你的回答。我不知道where子句的这种格式。它将来肯定会有帮助。$title=Character::find($selected_char->id)->title()->where('title',array('City',Castle')->get();为我工作。谢谢你的回答。我不知道where子句的这种格式。它将来肯定会有帮助。我确实找到了一个方法,正如我在第一次回答时的一条评论中所描述的。谢谢你的提示。@Serban
->其中
这正是SQL中
运算符中的
,顺便说一句,我的回答是在他之前…希望您理解为什么这在sql逻辑中不起作用。我确实找到了一个方法,正如我在第一次回答时的一条评论所述。感谢您的指示。@Serban
->其中
这正是sql中的
in
运算符,顺便说一句,我在他之前回答了这个问题…希望您理解为什么这在sql中不起作用逻辑。