查询语言.union

函数原型

  1. public function union($mixSelect = [], $sType = 'UNION');

说明:参数支持字符串、子查询器以及它们构成的一维数组。

用法如下

  1. /*
  2. SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test`
  3. UNION SELECT `yyyyy`.`yid` AS `id`,`yyyyy`.`name` AS `value` FROM `yyyyy` WHERE `yyyyy`.`first_name` = '222'
  4. UNION SELECT id,value FROM test2
  5. UNION SELECT `yyyyy`.`yid` AS `id`,`yyyyy`.`name` AS `value` FROM `yyyyy` WHERE `yyyyy`.`first_name` = '222'
  6. */
  7. $union1 = Db::table('yyyyy', 'yid as id,name as value')->where('first_name', '=', '222');
  8. $union2 = 'SELECT id,value FROM test2';
  9. Db::table('test', 'tid as id,tname as value')->
  10. union($union1)->
  11. union($union2)->
  12. union($union1)->
  13. getAll();

等价数组写法

  1. Db::table('test', 'tid as id,tname as value')->
  2. union([$union1, $union2, $union1])->
  3. getAll();

也支持 unionAll

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

  1. /*
  2. SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test`
  3. UNION ALL SELECT id,value FROM test2
  4. */
  5. $union1 = 'SELECT id,value FROM test2';
  6. Db::table('test', 'tid as id,tname as value')->
  7. unionAll($union1)->
  8. getAll();