正常情况下,以下这个语句是没有问题的:

    1. $sq = Db::table('test')->field($field)->where($where);
    2. $sq->union(function ($query) use ($option,$field,$where) {
    3. $query->table('test2')->field($field)->where($where);
    4. }, true);
    5. $sq->union(function ($query) use ($option,$field,$where) {
    6. $query->table('test3')->field($field)->where($where);
    7. }, true);
    8. $sq->order($order)->page($curpage,$pagesize)->select()->toArray();

    但如果要使用paginate来分页就会报错
    //todo 忘了错误了
    这种情况就得使用子查询,如下:

    1. $sq = Db::table('test')->field($field)->where($where);
    2. $sq->union(function ($query) use ($option,$field,$where) {
    3. $query->table('test2')->field($field)->where($where);
    4. }, true);
    5. $sq->union(function ($query) use ($option,$field,$where) {
    6. $query->table('test3')->field($field)->where($where);
    7. }, true);
    8. //这儿得再用子查询,否则直接用paginate会出错
    9. $subQuery = $sq->buildSql();
    10. $query = Db::table($subQuery . ' s');
    11. $data = $query->order(['s.id' => 'desc'])->paginate($option->pagesize)->toArray();