码迷,mamicode.com
首页 > 其他好文 > 详细

laraval join 的理解

时间:2019-08-15 12:48:49      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:can   stat   inner   more   query   sele   not   column   closure   

public function join($table, $one, $operator = null, $two = null, $type = ‘inner‘, $where = false)
{
// If the first "column" of the join is really a Closure instance the developer
// is trying to build a join with a complex "on" clause containing more than
// one condition, so we‘ll add the join and call a Closure with the query.
if ($one instanceof Closure) {
$join = new JoinClause($type, $table);
call_user_func($one, $join);
$this->joins[] = $join;
$this->addBinding($join->bindings, ‘join‘);
}
// If the column is simply a string, we can assume the join simply has a basic
// "on" clause with a single condition. So we will just build the join with
// this simple join clauses attached to it. There is not a join callback.
else {
$join = new JoinClause($type, $table);
$this->joins[] = $join->on(
$one, $operator, $two, ‘and‘, $where
);
$this->addBinding($join->bindings, ‘join‘);
}
return $this;
}

====DB::table(‘app_a as a‘)
->join(‘app_b as b‘,function($join){
$join->on(‘a.id‘,‘=‘,‘b.goodId‘)
->where(‘b.status‘,‘=‘,‘SUCCESS‘)
->where(‘b.type‘,‘=‘,‘UNLOCK‘);
}, null,null,‘left‘)
->where(‘a.id‘,‘>‘,1)
->get();

//相当于
SELECT * FROM app_a as a
LEFT JOIN app_b as b on a.id = b.goodId
and b.status = ‘SUCCESS‘ and b.type = ‘UNLOCK‘
where a.id > 1;


当join不传left时,默认是inner。

laraval join 的理解

标签:can   stat   inner   more   query   sele   not   column   closure   

原文地址:https://www.cnblogs.com/JdsyJ/p/11357104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!