SQL查询排序问题:确保结果一致性
{"title":"SQL查询排序问题:确保结果一致性","description":"本文探讨了SQL查询中ORDER BY子句的使用问题,并解释了如何确保两条查询语句的排序结果一致。","keywords":"SQL, ORDER BY, 排序, 查询结果, 一致性","content":""select x.C_CITID, x.C_COUID, x.C_TOWID, x.C_VILID, x.C_NATID, x.C_ID, x.C_CITNAME, x.C_COUNAME, x.C_TOWNAME, x.C_VILNAME, x.C_NAME, x.C_PROJECTNAME, sum(x.C_FINISH) AS C_FINISH, sum(x.C_NOFINISH) AS C_NOFINISH, sum(x.m9) as m9, sum(x.m1) as m1, sum(x.m2) as m2, sum(x.m3) as m3, sum(x.m4) as m4, sum(x.m5) as m5, sum(x.m6) as m6, sum(x.m7) as m7, sum(x.m8) as m8 \nfrom ( select n.C_CITID, n.C_COUID, n.C_TOWID, n.C_VILID, n.C_ID as C_NATID, x.C_ID, n.C_CITNAME, n.C_COUNAME, n.C_TOWNAME, n.C_VILNAME, n.C_NAME, x.C_PROJECTNAME, if(x.C_STATUS=1,1,0) as C_FINISH, if(x.C_STATUS=0,1,0) as C_NOFINISH, sum(y.m9) as m9, sum(y.m1) as m1, sum(y.m2) as m2, sum(y.m3) as m3, sum(y.m4) as m4, sum(y.m5) as m5, sum(y.m6) as m6, sum(y.m7) as m7, sum(y.m8) as m8 from db_twenty.t_project x LEFT JOIN db_twenty.t_natural n on(x.R_NAT_ID = n.C_ID) LEFT join db_twenty.t_projectreports y on(x.C_ID = y.R_PROJECT_ID and y.C_ANNUAL100+y.C_PERIOD BETWEEN 201909 and 202304 and y.C_DELETED = 0 ) where x.C_TOWID = 441424137000 and x.C_AUDITSTATUS = 1 and x.C_DELETED = 0 GROUP BY x.C_ID ) x GROUP BY x.C_VILID ORDER BY x.C_CITID,x.C_COUID,x.C_TOWID \n\n\nselect x.C_CITID, x.C_COUID, x.C_TOWID, x.C_VILID, x.C_NATID, x.C_ID, x.C_CITNAME, x.C_COUNAME, x.C_TOWNAME, x.C_VILNAME, x.C_NAME, x.C_PROJECTNAME, sum(x.C_FINISH), sum(x.C_NOFINISH), sum(x.m9) as m9, sum(x.m1) as m1, sum(x.m2) as m2, sum(x.m3) as m3, sum(x.m4) as m4, sum(x.m5) as m5, sum(x.m6) as m6, sum(x.m7) as m7, sum(x.m8) as m8 \nfrom ( select n.C_CITID, n.C_COUID, n.C_TOWID, n.C_VILID, n.C_ID as C_NATID, x.C_ID, n.C_CITNAME, n.C_COUNAME, n.C_TOWNAME, n.C_VILNAME, n.C_NAME, x.C_PROJECTNAME, if(x.C_STATUS=1,1,0) as C_FINISH, if(x.C_STATUS=0,1,0) as C_NOFINISH, sum(y.m9) as m9, sum(y.m1) as m1, sum(y.m2) as m2, sum(y.m3) as m3, sum(y.m4) as m4, sum(y.m5) as m5, sum(y.m6) as m6, sum(y.m7) as m7, sum(y.m8) as m8 from db_twenty.t_project x LEFT JOIN db_twenty.t_natural n on(x.R_NAT_ID = n.C_ID) LEFT join db_twenty.t_projectreports y on(x.C_ID = y.R_PROJECT_ID and y.C_ANNUAL100+y.C_PERIOD BETWEEN 201909 and 202304 and y.C_DELETED = 0 ) where x.C_TOWID = 441424137000 and x.C_AUDITSTATUS = 1 and x.C_DELETED = 0 GROUP BY x.C_ID ) x GROUP BY x.C_VILID ORDER BY x.C_CITID,x.C_COUID,x.C_TOWID \n上面一条查询SQL排序与下面一条查询SQL排序为什么不同,需要上面的效果跟下面的效果相同怎么处理内容:上面一条查询SQL使用了ORDER BY子句对结果进行排序,但只指定了部分列进行排序,即C_CITID、C_COUID和C_TOWID列。而下面一条查询SQL没有使用ORDER BY子句,因此结果的排序是不确定的。\n\n如果需要上面的查询结果与下面的查询结果相同,可以在上面的查询SQL中加入缺少的排序列,即C_NATID列。修改后的查询SQL如下:\n\n\nselect x.C_CITID, x.C_COUID, x.C_TOWID, x.C_VILID, x.C_NATID, x.C_ID, x.C_CITNAME, x.C_COUNAME, x.C_TOWNAME, x.C_VILNAME, x.C_NAME, x.C_PROJECTNAME, sum(x.C_FINISH), sum(x.C_NOFINISH), sum(x.m9) as m9, sum(x.m1) as m1, sum(x.m2) as m2, sum(x.m3) as m3, sum(x.m4) as m4, sum(x.m5) as m5, sum(x.m6) as m6, sum(x.m7) as m7, sum(x.m8) as m8 \nfrom ( select n.C_CITID, n.C_COUID, n.C_TOWID, n.C_VILID, n.C_ID as C_NATID, x.C_ID, n.C_CITNAME, n.C_COUNAME, n.C_TOWNAME, n.C_VILNAME, n.C_NAME, x.C_PROJECTNAME, if(x.C_STATUS=1,1,0) as C_FINISH, if(x.C_STATUS=0,1,0) as C_NOFINISH, sum(y.m9) as m9, sum(y.m1) as m1, sum(y.m2) as m2, sum(y.m3) as m3, sum(y.m4) as m4, sum(y.m5) as m5, sum(y.m6) as m6, sum(y.m7) as m7, sum(y.m8) as m8 from db_twenty.t_project x LEFT JOIN db_twenty.t_natural n on(x.R_NAT_ID = n.C_ID) LEFT join db_twenty.t_projectreports y on(x.C_ID = y.R_PROJECT_ID and y.C_ANNUAL*100+y.C_PERIOD BETWEEN 201909 and 202304 and y.C_DELETED = 0 ) where x.C_TOWID = 441424137000 and x.C_AUDITSTATUS = 1 and x.C_DELETED = 0 GROUP BY x.C_ID ) x GROUP BY x.C_VILID ORDER BY x.C_CITID,x.C_COUID,x.C_TOWID,x.C_NATID\n\n\n在ORDER BY子句中添加了x.C_NATID列,以保持与下面一条查询SQL的排序效果相同。"}
原文地址: https://www.cveoy.top/t/topic/pReD 著作权归作者所有。请勿转载和采集!