因为项目需求必须使用union关联查询,源代码如下:
$data = $student_model->field(implode(',', $fieldArr))->alias('student_sell')
->join($commonJoinArr)
->where($commonWhereArr)->order($order)->group($group)
->where($where)
->unionAll([$this->getUnionAllSql($request, $type, $flow_type, 0)])
->paginate();
其中getUnionAllSql代码如下(也就是生成需要执行的sql语句,unionAll里面只需要传入sql语句即可):
$sql = $student_model->field(implode(',', $fieldArr))->alias('student_sell')
->join($commonJoinArr)
->where($commonWhereArr)->order($order)->group($group)
->where($where)
->buildSql();
return $sql;
这里的$data最终是用paginate函数自动生成分页,但是这里报错了,使用select就可以正常获取到值。
于是开始懵逼,按理说就是这样子,后面又把查询方式换成了子查询的方式
$data = $student_model->field(implode(',', $fieldArr))->alias('student_sell')
->join($commonJoinArr)
->where($commonWhereArr)->order($order)->group($group)
->where($where)
->unionAll([$this->getUnionAllSql($request, $type, $flow_type, 0)])
->buildSql();
Db::table("$data ss")->paginate();
构建出来的sql语句本来希望是这种
SELECT
`student_sell`.*, student_sell.id id,
`student_sell`.`s