Laravel 多个条件外左连接与外部变量
Laravel multiple conditions outer left join with external variables
早上好,我有以下多个连接。我想要它作为左外。它对我不起作用...因为它正在将外部连接更改为内部连接。
$query=DB::Table("Users")->select("Users.*","Strings.Text as Title","Strings2.Text as Name" ,"Strings3.Text as Description", "Strings4.Text as Summary")
->join("Strings",function($join) use($bbddlang)
{
$join->on("Users.HeroTitle","Strings.StringID");
$join->where("Strings.Lang",$bbddlang);
}, "left outer")
->join("Strings as Strings2",function($join) use($bbddlang)
{
$join->on("Users.HeroName","Strings2.StringID");
$join->where("Strings2.Lang",$bbddlang);
}, "left outer")
->join("Strings as Strings3",function($join) use($bbddlang)
{
$join->on("Users.Description","Strings3.StringID");
$join->where("Strings3.Lang",$bbddlang);
}, "left outer")
->join("Strings as Strings4",function($join) use($bbddlang)
{
$join->on("Users.Summary","Strings4.StringID");
$join->where("Strings4.Lang",$bbddlang);
}, "left outer")
->where("Strings.Text",$hero);
这是我使用 ToSql() 方法得到的:
select `Users`.*, `Strings`.`Text` as `Title`, `Strings2`.`Text` as `Name`, `Strings3`.`Text` as `Description`, `Strings4`.`Text` as `Summary`
from `Users`
inner join `Strings`
on `Users`.`HeroTitle` = `Strings`.`StringID`
and `Strings`.`Lang` = ?
inner join `Strings` as `Strings2`
on `Users`.`HeroName` = `Strings2`.`StringID`
and `Strings2`.`Lang` = ?
inner join `Strings` as `Strings3`
on `Users`.`Description` = `Strings3`.`StringID`
and `Strings3`.`Lang` = ?
inner join `Strings` as `Strings4`
on `Users`.`Summary` = `Strings4`.`StringID`
and `Strings4`.`Lang` = ?
where `Strings`.`Text` = ?
(? 是查询的外部变量,这不是问题所在)。
当是单一条件时,是这样的
->join('answers as answers', 'responses.answer_id', '=', 'answers.id', 'left outer')
我已经尝试过使用 leftjoin,它将 inner join 替换为 left join,但是将参数传递给 outer 再次无用...因为 outer 没有出现在查询中。
我的问题是为什么忽略传递的 "outer join" 参数?。当第二个连接参数是一个函数时,我会错过另一个参数吗?
join(string $table, string $first, string|null $operator = null, string|null $second = null, string $type = 'inner', bool $where = false)
$first
可以是 Closure 的实例,所以当你使用闭包作为第二个参数时,"left join"
作为 $operator
的第三个参数,$type
具有默认值"inner"
。这就是为什么原始 sql 总是 return inner join ...
.
您需要将 LEFT OUTER
放在第 5 个位置:
->join("Strings", function($join) use($bbddlang)
{
$join->on("Users.HeroTitle","Strings.StringID");
$join->where("Strings.Lang",$bbddlang);
}, null, null, "left outer")
因此建议使用 leftjoin
with Closure 而不是 join
->leftjoin("Strings", function($join) use($bbddlang)
{
$join->on("Users.HeroTitle","Strings.StringID");
$join->where("Strings.Lang",$bbddlang);
})
早上好,我有以下多个连接。我想要它作为左外。它对我不起作用...因为它正在将外部连接更改为内部连接。
$query=DB::Table("Users")->select("Users.*","Strings.Text as Title","Strings2.Text as Name" ,"Strings3.Text as Description", "Strings4.Text as Summary")
->join("Strings",function($join) use($bbddlang)
{
$join->on("Users.HeroTitle","Strings.StringID");
$join->where("Strings.Lang",$bbddlang);
}, "left outer")
->join("Strings as Strings2",function($join) use($bbddlang)
{
$join->on("Users.HeroName","Strings2.StringID");
$join->where("Strings2.Lang",$bbddlang);
}, "left outer")
->join("Strings as Strings3",function($join) use($bbddlang)
{
$join->on("Users.Description","Strings3.StringID");
$join->where("Strings3.Lang",$bbddlang);
}, "left outer")
->join("Strings as Strings4",function($join) use($bbddlang)
{
$join->on("Users.Summary","Strings4.StringID");
$join->where("Strings4.Lang",$bbddlang);
}, "left outer")
->where("Strings.Text",$hero);
这是我使用 ToSql() 方法得到的:
select `Users`.*, `Strings`.`Text` as `Title`, `Strings2`.`Text` as `Name`, `Strings3`.`Text` as `Description`, `Strings4`.`Text` as `Summary`
from `Users`
inner join `Strings`
on `Users`.`HeroTitle` = `Strings`.`StringID`
and `Strings`.`Lang` = ?
inner join `Strings` as `Strings2`
on `Users`.`HeroName` = `Strings2`.`StringID`
and `Strings2`.`Lang` = ?
inner join `Strings` as `Strings3`
on `Users`.`Description` = `Strings3`.`StringID`
and `Strings3`.`Lang` = ?
inner join `Strings` as `Strings4`
on `Users`.`Summary` = `Strings4`.`StringID`
and `Strings4`.`Lang` = ?
where `Strings`.`Text` = ?
(? 是查询的外部变量,这不是问题所在)。
当是单一条件时,是这样的
->join('answers as answers', 'responses.answer_id', '=', 'answers.id', 'left outer')
我已经尝试过使用 leftjoin,它将 inner join 替换为 left join,但是将参数传递给 outer 再次无用...因为 outer 没有出现在查询中。
我的问题是为什么忽略传递的 "outer join" 参数?。当第二个连接参数是一个函数时,我会错过另一个参数吗?
join(string $table, string $first, string|null $operator = null, string|null $second = null, string $type = 'inner', bool $where = false)
$first
可以是 Closure 的实例,所以当你使用闭包作为第二个参数时,"left join"
作为 $operator
的第三个参数,$type
具有默认值"inner"
。这就是为什么原始 sql 总是 return inner join ...
.
您需要将 LEFT OUTER
放在第 5 个位置:
->join("Strings", function($join) use($bbddlang)
{
$join->on("Users.HeroTitle","Strings.StringID");
$join->where("Strings.Lang",$bbddlang);
}, null, null, "left outer")
因此建议使用 leftjoin
with Closure 而不是 join
->leftjoin("Strings", function($join) use($bbddlang)
{
$join->on("Users.HeroTitle","Strings.StringID");
$join->where("Strings.Lang",$bbddlang);
})