正常情况下,以下这个语句是没有问题的:
$sq = Db::table('test')->field($field)->where($where);
$sq->union(function ($query) use ($option,$field,$where) {
$query->table('test2')->field($field)->where($where);
}, true);
$sq->union(function ($query) use ($option,$field,$where) {
$query->table('test3')->field($field)->where($where);
}, true);
$sq->order($order)->page($curpage,$pagesize)->select()->toArray();
但如果要使用paginate
来分页就会报错
//todo 忘了错误了
这种情况就得使用子查询,如下:
$sq = Db::table('test')->field($field)->where($where);
$sq->union(function ($query) use ($option,$field,$where) {
$query->table('test2')->field($field)->where($where);
}, true);
$sq->union(function ($query) use ($option,$field,$where) {
$query->table('test3')->field($field)->where($where);
}, true);
//这儿得再用子查询,否则直接用paginate会出错
$subQuery = $sq->buildSql();
$query = Db::table($subQuery . ' s');
$data = $query->order(['s.id' => 'desc'])->paginate($option->pagesize)->toArray();