错误代码:1111无效使用组功能

时间:2017-11-21 00:08:14

标签: mysql sql

您好我有这个查询

SELECT perf_prog.date, MAX(perf_prog.end_time) , ADDTIME((shift_time.out), "01:00:00") FROM perf_prog 
INNER JOIN Shifts ON perf_prog.emp_id = Shifts.emp_id 
INNER JOIN shift_time ON Shifts.id = shift_time.id

WHERE perf_prog.emp_id = 920   GROUP BY perf_prog.date  

这是查询的输出

enter image description here

但我想只列出end_time的值大于shift_time.out

所以我做了这个

SELECT perf_prog.date, MAX(perf_prog.end_time) , ADDTIME((shift_time.out), "01:00:00") FROM perf_prog 
INNER JOIN Shifts ON perf_prog.emp_id = Shifts.emp_id 
INNER JOIN shift_time ON Shifts.id = shift_time.id

WHERE perf_prog.emp_id = 920 AND MAX(perf_prog.end_time) > ADDTIME((shift_time.out), "01:00:00")   GROUP BY perf_prog.date  

但我收到错误

Error Code: 1111
Invalid use of group function

并且在搜索stackoverflow时解决问题的答案是将条件移动到有子句

所以我得到了这个

SELECT perf_prog.date, MAX(perf_prog.end_time) , ADDTIME((shift_time.out), "01:00:00") FROM perf_prog 
INNER JOIN Shifts ON perf_prog.emp_id = Shifts.emp_id 
INNER JOIN shift_time ON Shifts.id = shift_time.id

WHERE perf_prog.emp_id = 920 GROUP BY perf_prog.date  
HAVING MAX(perf_prog.end_time)   > ADDTIME((shift_time.out), "01:00:00")

现在问题是Unknown column 'shift_time.out' in 'having clause'

我是否按照我的需要提供了正确的解决方案?谢谢。我一直试图找出解决方案,但我无法使其发挥作用

1 个答案:

答案 0 :(得分:2)

您需要在列上进行汇总。我想这可能是你想要的:

SELECT pp.date, MAX(pp.end_time),
       MAX(ADDTIME((st.out), '01:00:00'))
FROM perf_prog pp INNER JOIN
     Shifts s
     ON pp.emp_id = s.emp_id INNER JOIN
     shift_time st
     ON s.id = st.id
WHERE pp.emp_id = 920
GROUP BY pp.date  
HAVING MAX(pp.end_time) > MAX(ADDTIME((st.out), '01:00:00))