salut 2022-02-15 11:37 采纳率: 50%
浏览 49
已结题

mysql查询出现查询条件影响查询字段值的诡异问题求解

问题遇到的现象和发生背景

一段由多个表组成的查询结果,根据若干个字段判断当前数据的状态:

img

当不增加任何条件的时候查出来一条是“已收齐”,看到部门名称是“业支领导”:

img

当添加筛选条件,要搜索状态为“已收齐”当数据的时候,查到的数据的确是原来“业支领导”,但是状态却变成了“未开始”,这是为什么,why?命名group_id还是存在的啊!

img

问题相关代码,请勿粘贴截图

原始代码:

  1. select * from (
  2. select
  3. case when group_checked_je>=je then '已完成'
  4. when checked_cnt>0 then '已完成'
  5. when a11.group_id is null then '未开始'
  6. when a11.je_gets<je then '未收齐'
  7. when a11.je_gets>=je then '已收齐'
  8. end group_status,
  9. case when a11.group_id is not null then a11.group_id else concat(a11.cycle,a11.dept_id) end id,
  10. a11.cycle,a11.dept_id,a11.parent_id,a11.dept_name,a11.dept_long_name,a11.group_id,
  11. a11.manager,a11.do_time_begin,a11.do_time_end,a11.do_times,
  12. a11.cnt_dept,
  13. a11.cnt,
  14. a11.je,
  15. a11.cnt_gets,
  16. a11.je_gets,
  17. a11.left_full,
  18. a11.left_poor,
  19. a11.remain,
  20. a11.group_checked_je,
  21. eff_get_fp_cnt,all_get_fp_cnt,
  22. a11.can_gets_cnt,
  23. checked_cnt
  24. from
  25. (
  26. select a10.cycle,a10.dept_id,a10.parent_id,a10.dept_name,a10.dept_long_name,a10.group_id,
  27. a10.manager,a10.do_time_begin,a10.do_time_end,a10.do_times,
  28. count(0) cnt_dept,
  29. sum(case when a10.bxed0>0 then 1 else 0 end) cnt,
  30. sum(a10.bxed0) je,
  31. sum(case when a10.bxed0>0 and a10.je_need=0 then 1 else 0 end) cnt_gets,
  32. sum(user_je_eff) je_gets,
  33. sum(case when a10.bxed0>0 and a10.je_need >0 and a10.user_left_fp_je>= a10.je_need then 1 else 0 end) left_full,
  34. sum(case when a10.bxed0>0 and a10.je_need >0 and a10.user_left_fp_je< a10.je_need then 1 else 0 end) left_poor,
  35. sum(case when a10.je_need>0 then 1 else 0 end) remain,
  36. sum(a10.user_checked_je) group_checked_je,
  37. sum(a10.eff_get_fp_cnt) eff_get_fp_cnt,sum(a10.all_get_fp_cnt) all_get_fp_cnt,
  38. sum(a10.checked_cnt) checked_cnt,
  39. sum(a10.can_gets_cnt) can_gets_cnt
  40. from
  41. (
  42. select a9.cycle,a9.dept_id,a9.parent_id,a9.dept_name,a9.dept_long_name,a9.user_id,a9.login_name,
  43. a9.user_name, a9.bxed,a9.bxed0,a9.group_id,a9.manager,a9.do_time_begin,a9.do_time_end,a9.do_times,
  44. a9.user_je_eff,
  45. a9.user_je_exp,
  46. a9.user_checked_je,
  47. a9.eff_get_fp_cnt,
  48. a9.all_get_fp_cnt,
  49. a9.checked_cnt,
  50. ifnull( sum(b9.jshjxx) ,0) user_left_fp_je,
  51. case when sum(b9.jshjxx) =null then 0 else count(b9.id) end user_left_fp_cnt,
  52. case when a9.bxed0<=0 then 0
  53. when a9.bxed0>0 and a9.user_je_eff>= a9.bxed0 then 0
  54. else a9.bxed0-a9.user_je_eff end je_need,
  55. case when (case when a9.bxed0 <=0 then 0
  56. when a9.bxed0>0 and a9.user_je_eff>= a9.bxed0 then 0
  57. else a9.bxed0-a9.user_je_eff end)>0
  58. and (ifnull( sum(b9.jshjxx) ,0))>0 then 1 else 0 end can_gets_cnt
  59. from
  60. (
  61. select a8.cycle,a8.dept_id,a8.parent_id,a8.dept_name,a8.dept_long_name,a8.user_id,a8.login_name,
  62. a8.user_name, a8.bxed,a8.group_id,a8.manager,a8.do_time_begin,a8.do_time_end,a8.do_times,a8.bxed0,
  63. sum(case when a8.check_status <> '-1' then a8.jshjxx else 0 end) user_je_eff,
  64. sum(case when a8.check_status = '-1' then a8.jshjxx else 0 end) user_je_exp,
  65. sum(case when a8.check_status='1' then a8.jshjxx else 0 end) user_checked_je,
  66. sum(case when a8.check_status <> '-1' then 1 else 0 end) eff_get_fp_cnt,
  67. sum(case when a8.check_status is not null then 1 else 0 end) all_get_fp_cnt,
  68. sum(case when a8.check_status is not null and check_status='1' then 1 else 0 end) checked_cnt
  69. from
  70. (
  71. select a7.cycle,a7.dept_id,a7.parent_id,a7.dept_name,a7.dept_long_name,a7.user_id,a7.login_name,
  72. a7.user_name, a7.bxed,a7.group_id,a7.manager,a7.do_time_begin,a7.do_time_end,a7.do_times
  73. ,a7.inv_id,a7.check_status,b7.jshjxx,a7.bxed0 from
  74. (
  75. select a6.cycle,a6.dept_id,a6.parent_id,a6.dept_name,a6.dept_long_name,a6.user_id,a6.login_name,
  76. a6.user_name, a6.bxed,a6.group_id,a6.manager,a6.do_time_begin,a6.do_time_end,a6.do_times
  77. ,b6.inv_id,b6.status check_status,a6.bxed0 from
  78. (
  79. select a5.cycle,a5.dept_id,a5.parent_id,a5.dept_name,a5.dept_long_name,a5.user_id,a5.login_name,
  80. a5.user_name, a5.bxed,
  81. case when b5.bxed0 is null then a5.bxed when b5.bxed0=0 then 0 else a5.bxed end bxed0,
  82. a5.group_id,a5.manager,a5.do_time_begin,a5.do_time_end,a5.do_times
  83. ,b5.id igu_id from
  84. (
  85. select a4.cycle,a4.dept_id,a4.parent_id,a4.dept_name,a4.dept_long_name,a4.user_id,a4.login_name,
  86. a4.user_name, a4.bxed,b4.id group_id,b4.manager,b4.do_time_begin,b4.do_time_end,b4.do_times
  87. from
  88. (
  89. select a3.cycle,a3.dept_id,a3.parent_id,a3.dept_name,a3.dept_long_name,a3.user_id,a3.login_name,a3.user_name,sum(a3.bxed) bxed from
  90. (
  91. select a2.cycle,a2.dept_id,a2.parent_id,a2.dept_name,a2.dept_long_name,a2.user_id,a2.login_name,a2.user_name,b2.bxed from
  92. (
  93. select a1.cycle,a1.dept_id,a1.parent_id,a1.dept_name,a1.dept_long_name,a1.user_id,a1.login_name,a1.user_name,b1.post_id from
  94. (
  95. select '2022-01' cycle,a.dept_id,a.parent_id,a.dept_name,a.leader dept_long_name,b.user_id,b.login_name,b.user_name
  96. from sys_dept a
  97. left join sys_user as b
  98. on a.dept_id=b.dept_id
  99. where a.status='0'
  100. and b.status='0'
  101. and a.del_flag='0'
  102. and b.del_flag = '0'
  103. and a.parent_id=101
  104. ) a1 left join sys_user_post b1
  105. on a1.user_id=b1.user_id
  106. )a2 left join sys_post b2
  107. on a2.post_id=b2.post_id
  108. where b2.status='0'
  109. ) a3
  110. group by a3.cycle,a3.dept_id,a3.dept_name,a3.dept_long_name,a3.user_id,a3.login_name,a3.user_name
  111. )a4 left join invoice_gets_group b4
  112. on a4.cycle=b4.cycle
  113. and a4.dept_id=b4.dept_id
  114. ) a5 left join invoice_gets_user b5
  115. on a5.group_id =b5.group_id
  116. and a5.login_name =b5.login_name
  117. )a6 left join invoice_gets_user_list b6
  118. on a6.igu_id= b6.igu_id
  119. )a7 left join invoice b7
  120. on a7.inv_id=b7.id
  121. )a8
  122. group by a8.cycle,a8.dept_id,a8.parent_id,a8.dept_name,a8.dept_long_name,a8.user_id,a8.login_name,
  123. a8.user_name, a8.bxed,a8.group_id,a8.manager,a8.do_time_begin,a8.do_time_end,a8.do_times,a8.bxed0
  124. )a9 left join invoice b9
  125. on a9.login_name=b9.user_name
  126. and b9.status = '1'
  127. and b9.kprq like concat(substr('2022-01',1,4),'%')
  128. group by a9.cycle,a9.dept_id,a9.parent_id,a9.dept_name,a9.dept_long_name,a9.user_id,a9.login_name,
  129. a9.user_name, a9.bxed,a9.bxed0,a9.group_id,a9.manager,a9.do_time_begin,a9.do_time_end,a9.do_times,
  130. a9.user_je_eff,
  131. a9.user_je_exp
  132. )a10
  133. group by a10.cycle,a10.dept_id,a10.parent_id,a10.dept_name,a10.dept_long_name,a10.group_id,
  134. a10.manager,a10.do_time_begin,a10.do_time_end,a10.do_times
  135. )a11) a12
  136. where a12.group_status='已收齐'
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

我就想知道查出来后为什么结果中字段值变了?

展开全部

  • 写回答

2条回答 默认 最新

  • salut 2022-02-15 15:47
    关注

    不知原因,但是这么写是能去到,感谢老同学HZG:

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月15日
  • 创建了问题 2月15日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部