mysql 子查询分组, 父查询排序+分页 10
下面是我的SQLSELECT*,if(org_user_num=0,0.0000,round(local_audit_user_num/org_user_num,4))a...
下面是我的SQL
SELECT *,if(org_user_num=0,0.0000,round(local_audit_user_num/org_user_num,4)) as user_rate,if(local_into_num=0,0.00,round(local_back_num/local_into_num,2)) as local_back_rate,if(saler_into_num=0,0.00,round(saler_back_num/saler_into_num,2)) as saler_back_rate,if(local_audit_user_num=0,0.00,round(saler_back_num/local_audit_user_num,2)) as saler_back_local_rate FROM ( SELECT `org_id`,`org_code`,`org_level`,round(avg(org_user_num)) as org_user_num,round(avg(local_audit_user_num)) as local_audit_user_num,sum(local_into_num) as local_into_num,sum(saler_into_num) as saler_into_num,sum(local_back_num) as local_back_num,sum(saler_back_num) as saler_back_num FROM `contract_back_count` WHERE ( (`create_date` BETWEEN 1448380800 AND 1448380800 ) ) GROUP BY org_code ) sub WHERE ( `org_level` = 4 ) AND ( `org_code` LIKE '01%' ) ORDER BY `saler_back_local_rate` asc LIMIT 0,10;
我的问题是: 这条sql 如果不排序,分页查询的时候,每一页的数据都是正确的,没问题!
但是进行排序后, 分页查询的时候, 有一段部分数据,在不同的页中重复出现,但是如果不加 limit 条件,一次全部查出, 数据就不会重复,是正确的!
我现在纠结的就是, 到底是分页有问题, 还是排序有问题, 不知道要怎么解决了!
为什么对子查询分组后的结果,进行排序,分页后数据会不准确呢?
希望大神帮我解决, 展开
SELECT *,if(org_user_num=0,0.0000,round(local_audit_user_num/org_user_num,4)) as user_rate,if(local_into_num=0,0.00,round(local_back_num/local_into_num,2)) as local_back_rate,if(saler_into_num=0,0.00,round(saler_back_num/saler_into_num,2)) as saler_back_rate,if(local_audit_user_num=0,0.00,round(saler_back_num/local_audit_user_num,2)) as saler_back_local_rate FROM ( SELECT `org_id`,`org_code`,`org_level`,round(avg(org_user_num)) as org_user_num,round(avg(local_audit_user_num)) as local_audit_user_num,sum(local_into_num) as local_into_num,sum(saler_into_num) as saler_into_num,sum(local_back_num) as local_back_num,sum(saler_back_num) as saler_back_num FROM `contract_back_count` WHERE ( (`create_date` BETWEEN 1448380800 AND 1448380800 ) ) GROUP BY org_code ) sub WHERE ( `org_level` = 4 ) AND ( `org_code` LIKE '01%' ) ORDER BY `saler_back_local_rate` asc LIMIT 0,10;
我的问题是: 这条sql 如果不排序,分页查询的时候,每一页的数据都是正确的,没问题!
但是进行排序后, 分页查询的时候, 有一段部分数据,在不同的页中重复出现,但是如果不加 limit 条件,一次全部查出, 数据就不会重复,是正确的!
我现在纠结的就是, 到底是分页有问题, 还是排序有问题, 不知道要怎么解决了!
为什么对子查询分组后的结果,进行排序,分页后数据会不准确呢?
希望大神帮我解决, 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询