版权声明:本文为ywcmoon原创文章,未经允许不得转载。 https://blog.csdn.net/qq_39251267/article/details/82225578
合并两个或多个 SELECT 语句的结果集
Db::field('name')
->table('think_user_0')
->union('SELECT name FROM think_user_1')
->union('SELECT name FROM think_user_2')
->select();
数组用法:
Db::field('name')
->table('think_user_0')
->union(['SELECT name FROM think_user_1','SELECT name FROM think_user_2'])
->select();
闭包用法:
Db::field('name')
->table('think_user_0')
->union(function($query){
$query->field('name')->table('think_user_1');
})
->union(function($query){
$query->field('name')->table('think_user_2');
})
->select();
支持 UNION ALL :
Db::field('name')
->table('think_user_0')
->union('SELECT name FROM think_user_1',true)
->union('SELECT name FROM think_user_2',true)
->select();
Db::field('name')
->table('think_user_0')
->union(['SELECT name FROM think_user_1','SELECT name FROM think_user_2'],true)
->select();
注意:UNION 内部的 SELECT 语句必须拥有相同数量的列
。列也必须拥有相似的数据类型
。同时,每条 SELECT 语句中的列的顺序必须相同
。