因爲項目需求必須使用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`.`student_name`,
`school`.`school_name`,
`contract`.`status`,
`course`.`course_name`,
`class`.`class_name`,
`class`.`class_pa`,
`student_class`.`isdelete`,
`flow`.`auditor_content`,
`flow`.`create_time`,
`flow`.`data_id`,
`flow`.`type`,
`flow`.`auditor`,
`flow`.`next_person`,
flow.id flow_id,
flow.create_time create_time,
`change_class_record`.`flow_id`,
`flow`.`change_class_record_id`,
`flow`.`auditor_status`,
`flow`.`result_status`,
`flow`.`change_flow_type`,
`change_class_record`.`note`
FROM
`rms_student_sell` `student_sell`
INNER JOIN `rms_school` `school` ON `student_sell`.`school_id` = `school`.`id`
INNER JOIN `rms_student_sell_contract` `contract` ON `contract`.`student_sell_id` = `student_sell`.`id`
INNER JOIN `rms_student_course` `student_course` ON `student_course`.`student_id` = `student_sell`.`id`
INNER JOIN `rms_course` `course` ON `course`.`id` = student_course.course_id
AND contract.course_id = course.id
LEFT JOIN `rms_student_class` `student_class` ON `student_sell`.`id` = `student_class`.`student_id`
LEFT JOIN `rms_classs` `class` ON `student_class`.`class_id` = `class`.`id`
INNER JOIN `rms_change_class_record` `change_class_record` ON `change_class_record`.`old_student_id` = `student_sell`.`id`
INNER JOIN `rms_flow` `flow` ON `flow`.`data_id` = student_course.id
AND flow.change_class_record_id = change_class_record.id
WHERE
`student_sell`.`business_status` = '14'
AND `student_class`.`isdelete` = '0'
AND `flow`.`person_type` = '0'
AND `flow`.`auditor` = '1'
AND `flow`.`type` = '3'
AND `change_flow_type` = '1'
GROUP BY
`flow`.`id`
UNION
(
(
SELECT
`student_sell`.*, student_sell.id id,
`student_sell`.`student_name`,
`school`.`school_name`,
`contract`.`status`,
`course`.`course_name`,
`class`.`class_name`,
`class`.`class_pa`,
`student_class`.`isdelete`,
`flow`.`auditor_content`,
`flow`.`create_time`,
`flow`.`data_id`,
`flow`.`type`,
`flow`.`auditor`,
`flow`.`next_person`,
flow.id flow_id,
flow.create_time create_time,
`change_class_record`.`flow_id`,
`flow`.`change_class_record_id`,
`flow`.`auditor_status`,
`flow`.`result_status`,
`flow`.`change_flow_type`,
`change_class_record`.`note`
FROM
`rms_student_sell` `student_sell`
INNER JOIN `rms_school` `school` ON `student_sell`.`school_id` = `school`.`id`
INNER JOIN `rms_student_sell_contract` `contract` ON `contract`.`student_sell_id` = `student_sell`.`id`
LEFT JOIN `rms_student_class` `student_class` ON `student_sell`.`id` = `student_class`.`student_id`
LEFT JOIN `rms_classs` `class` ON `student_class`.`class_id` = `class`.`id`
INNER JOIN `rms_change_class_record` `change_class_record` ON `change_class_record`.`old_student_id` = `student_sell`.`id`
INNER JOIN `rms_course_renew` `course_renew` ON `course_renew`.`student_id` = `student_sell`.`id`
INNER JOIN `rms_course` `course` ON `course`.`id` = course_renew.course_id
AND contract.course_id = course.id
INNER JOIN `rms_flow` `flow` ON `flow`.`data_id` = course_renew.id
AND flow.change_class_record_id = change_class_record.id
WHERE
`student_sell`.`business_status` = '14'
AND `student_class`.`isdelete` = '0'
AND `flow`.`person_type` = '0'
AND `flow`.`auditor` = '1'
AND `flow`.`type` = '3'
AND `change_flow_type` = '3'
GROUP BY
`flow`.`id`
)
)
事實沒有想得那麼美好,只要使用paginate函數就會在前面自動加上,追蹤源碼
發現sql語句前面增加了
SELECT
COUNT(s.*) AS tp_count
FROM
這時候纔想到,分頁之前要求總條數,但是對於union查詢來說求count的時候字段會重複。
於是去官方文檔查看
果然傳入的分頁總條數,
$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(1, 2, ['query' => $request->param()]);
希望遇到坑的同學,可以跳出這個坑。