join (chain operation 11)
join usually has the following types. Different types of join operations will affect the returned data results.
1.INNER JOIN: Equivalent to JOIN (default JOIN type), if there is at least one match in the table, the row is returned
2.LEFT JOIN: Even if there is no match in the right table, Also returns all rows from the left table
3.RIGHT JOIN: Even if there is no match in the left table, returns all rows from the right table
4.FULL JOIN: As long as there is a match in one of the tables If there is a match, return the row
Description
join ( mixed join [, mixed $condition = null [, string $type = 'INNER']] ) leftJoin ( mixed join [, mixed $condition = null ] ) rightJoin ( mixed join [, mixed $condition = null ] ) fullJoin ( mixed join [, mixed $condition = null ] )
Parameters
join
要关联的(完整)表名以及别名
Supported writing methods:
Writing method 1: ['Complete table name or subquery'=>'alias']
Writing method 2:'Table name without data table prefix' (automatically used as an alias)
Writing 2: 'Table name alias without data table prefix'
condition
关联条件。可以为字符串或数组, 为数组时每一个元素都是一个关联条件。
type
关联类型。可以为:`INNER`、`LEFT`、`RIGHT`、`FULL`,不区分大小写,默认为`INNER`。
Return value
Model object
Example
Db::table('think_artist') ->alias('a') ->join('work w','a.id = w.artist_id') ->join('card c','a.card_id = c.id') ->select();
Db::table('think_user') ->alias('a') ->join(['think_work'=>'w'],'a.id=w.artist_id') ->join(['think_card'=>'c'],'a.card_id=c.id') ->select();
The INNER JOIN method is used by default. If you need to use other JOIN methods, you can change it to
Db::table('think_user') ->alias('a') ->leftJoin('word w','a.id = w.artist_id') ->select();
The table name can also be a subquery
$subsql = Db::table('think_work') ->where('status',1) ->field('artist_id,count(id) count') ->group('artist_id') ->buildSql(); Db::table('think_user') ->alias('a') ->join([$subsql=> 'w'], 'a.artist_id = w.artist_id') ->select();