查询语言.join

函数原型

  1. public function join($mixTable, $mixCols = '*', $mixCond /* args */);

$mixTable 和 $mixCols 与 table 方法中的用法一致

  1. # SELECT `test`.*,`hello`.`name`,`hello`.`value` FROM `test` INNER JOIN `hello` ON `test`.`name` = '小牛'
  2. Db::table('test')->
  3. join('hello', 'name,value', 'name', '=', '小牛')->
  4. getAll();
  5. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` INNER JOIN `hello` `t` ON `test`.`name` = '小牛'
  6. Db::table('test')->
  7. join(['t' => 'hello'], ['name as nikename', 'tt' => 'value'], 'name', '=', '小牛')->
  8. getAll();

$mixCond 与 where 方法中的用法一致

去掉前面两个参数,后面的全部都是条件参数。

  1. # SELECT `test`.*,`hello`.`name`,`hello`.`value` FROM `test` INNER JOIN `hello` ON `test`.`hello` = 'world' AND `test`.`test` > `test`.`name`
  2. Db::table('test')->
  3. join('hello', 'name,value', ['hello' => 'world', ['test', '>', '{[name]}']])->
  4. getAll();
  5. # SELECT `test`.*,`hello`.`name`,`hello`.`value` FROM `test` INNER JOIN `hello` ON (`hello`.`id` < 5 AND `hello`.`name` LIKE 'hello')
  6. Db::table('test')->
  7. join('hello', 'name,value', function ($select) {
  8. $select->where('id', '<', 5)->where('name', 'like', 'hello');
  9. }s)->
  10. getAll();

innerJoin 与 join 一致

  1. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` INNER JOIN `hello` `t` ON `t`.`name` = '小牛'
  2. Db::table('test')->
  3. innerJoin(['t' => 'hello'], ['name as nikename', 'tt' => 'value'], 'name', '=', '小牛')->
  4. getAll();

leftJoin

  1. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` LEFT JOIN `hello` `t` ON `t`.`name` = '小牛'
  2. Db::table('test')->
  3. innerJoin(['t' => 'hello'], ['name as nikename', 'tt' => 'value'], 'name', '=', '小牛')->
  4. getAll();

rightJoin

  1. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` RIGHT JOIN `hello` `t` ON `t`.`name` = '小牛'
  2. Db::table('test')->
  3. rightJoin(['t' => 'hello'], ['name as nikename', 'tt' => 'value'], 'name', '=', '小牛')->
  4. getAll();

fullJoin

  1. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` FULL JOIN `hello` `t` ON `t`.`name` = '小牛'
  2. Db::table('test')->
  3. fullJoin(['t' => 'hello'], ['name as nikename', 'tt' => 'value'], 'name', '=', '小牛')->
  4. getAll();

crossJoin

交叉连接不用设置 on 条件。

  1. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` CROSS JOIN `hello` `t`
  2. Db::table('test')->
  3. crossJoin(['t' => 'hello'], ['name as nikename', 'tt' => 'value'])->
  4. getAll();

naturalJoin

自然连接不用设置 on 条件。

  1. # SELECT `test`.*,`t`.`name` AS `nikename`,`t`.`value` AS `tt` FROM `test` NATURAL JOIN `hello` `t`
  2. Db::table('test')->
  3. naturalJoin(['t' => 'hello'], ['name as nikename', 'tt' => 'value'])->
  4. getAll();