当前位置:   article > 正文

SQL 左连接(left join) 排序 分页 中遇到的未按理想状态排序分页的解决方案_left join多表分页查询问题

left join多表分页查询问题
  1. SELECTa.id AS"id", a.code AS"code", a.nameAS "name", a.type AS"type", a.importance_degree AS"importanceDegree", a.tech_state AS"techState", a.customerid AS"customerid", a.departmentid AS"departmentid", a.project_managerid AS"projectManagerid", a.plan_starttime AS"planStarttime", a.plan_endtime AS"planEndtime", a.state AS"state", a.act_starttime AS"actStarttime", a.act_endtime AS"actEndtime", a.sending_time AS"sendingTime", a.old_plan_starttime AS"oldPlanStarttime", a.old_plan_endtime AS"oldPlanEndtime", a.delivery_time AS"deliveryTime", a.complete_status AS"completeStatus", a.marketerid AS"marketerid", a.create_by AS"createBy.id", a.create_date AS"createDate.id", a.update_by AS"updateBy.id", a.update_date AS"updateDate.id", a.remarks AS"remarks", a.del_flag AS"delFlag", department.id AS"department.id", department.nameAS "department.name", projectManager.id AS"projectManager.id", projectManager.nameAS "projectManager.name", projectManager.mobile AS"projectManager.mobile", marketer.id AS"marketer.id", marketer.nameAS "marketer.name", marketer.mobile AS"marketer.mobile", customer.id AS"customer.id", customer.code AS"customer.code", customer.nameAS "customer.name", push.id AS"push.id", push.projectid AS"push.project.id", push.userid AS"push.user.id", push.is_readed AS"push.isReaded"
  2. FROM(SELECT* FROMps_project WHEREdel_flag = 0 ORDERBY code DESClimit 2,2) a
  3. LEFTJOIN ps_customer customer ONa.customerid = customer.id
  4. LEFTJOIN sys_office department ONa.departmentid = department.id
  5. LEFTJOIN sys_user projectManager ONa.project_managerid = projectManager.id
  6. LEFTJOIN sys_user marketer ONa.marketerid = marketer.id
  7. LEFTJOIN ps_project_push push ONa.id = push.projectid
  8. AND push.del_flag = 0
  9. AND customer.del_flag= 0
  10. AND department.del_flag= 0
  11. AND projectManager.del_flag= 0
  12. AND marketer.del_flag= 0
语句目标:
    以主表排序后并进行分页,而后再去连接其它表
出现问题:
    最终主表并没有按照预想进行顺序输出,但是分页的数据是正确的。
来自   stackflow 解答:

No, the JOIN by order is changed during optimization.

我的解决办法(经测试有效)

分页排序在主表中进行,这样就mysql在执行的过程中分根据我们的理想按字段排序且选出指定分页。

但是在Join时,mysql系统做了优化,所以最终出来的结果又是乱序,此时,对最终被mysql Join打乱的结果顺序再做一次排序,这样就能得到我们想要的结果了。

  1. SELECTa.id AS"id", a.code AS"code", a.nameAS "name", a.type AS"type", a.importance_degree AS"importanceDegree", a.tech_state AS"techState", a.customerid AS"customerid", a.departmentid AS"departmentid", a.project_managerid AS"projectManagerid", a.plan_starttime AS"planStarttime", a.plan_endtime AS"planEndtime", a.state AS"state", a.act_starttime AS"actStarttime", a.act_endtime AS"actEndtime", a.sending_time AS"sendingTime", a.old_plan_starttime AS"oldPlanStarttime", a.old_plan_endtime AS"oldPlanEndtime", a.delivery_time AS"deliveryTime", a.complete_status AS"completeStatus", a.marketerid AS"marketerid", a.create_by AS"createBy.id", a.create_date AS"createDate.id", a.update_by AS"updateBy.id", a.update_date AS"updateDate.id", a.remarks AS"remarks", a.del_flag AS"delFlag", department.id AS"department.id", department.nameAS "department.name", projectManager.id AS"projectManager.id", projectManager.nameAS "projectManager.name", projectManager.mobile AS"projectManager.mobile", marketer.id AS"marketer.id", marketer.nameAS "marketer.name", marketer.mobile AS"marketer.mobile", customer.id AS"customer.id", customer.code AS"customer.code", customer.nameAS "customer.name", push.id AS"push.id", push.projectid AS"push.project.id", push.userid AS"push.user.id", push.is_readed AS"push.isReaded"
  2. FROM(SELECT* FROM ps_project WHEREdel_flag = 0 ORDERBY code DESC limit 0,2 ) a
  3. LEFT JOIN ps_customer customer ONa.customerid = customer.id
  4. LEFT JOIN sys_office department ONa.departmentid = department.id
  5. LEFT JOIN sys_user projectManager ONa.project_managerid = projectManager.id
  6. LEFT JOIN sys_user marketer ONa.marketerid = marketer.id
  7. LEFT JOIN ps_project_push push ONa.id = push.projectid
  8. ANDpush.del_flag = 0 ANDcustomer.del_flag= 0 ANDdepartment.del_flag= 0 ANDprojectManager.del_flag= 0 ANDmarketer.del_flag= 0
  9. ORDERBY code DESC


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/268597
推荐阅读
相关标签
  

闽ICP备14008679号