以下python语句sql = """ SELECT date_z, SUM(COUNT() + IFNULL(nn, 0)) FROM ( SELECT id_person, date_z, date_finish FROM luzhi_jilu_all WHERE id_person = %s AND (date_z = date_finish OR date_finish IS NULL) ) AS t1 LEFT JOIN ( SELECT id_person, date_finish, COUNT() AS nn FROM luzhi_jilu_all GROUP BY id_person, date_finish ) AS t2 ON t1.id_person = t2.id_person AND t1.date_z = t2.date_finish GROUP BY date_z ORDER BY date_z DESC """ params = (id_person,) cs.execute(sql, params) jg = cs.fetchall()出现以下错误提示,请检查,并给出修改pymysql.err.ProgrammingError: (1111, 'Invalid use of group function')
时间: 2024-02-14 08:16:53 浏览: 111
MySql版本问题sql_mode=only_full_group_by的完美解决方案
这个错误通常是由于 SQL 语句中的 GROUP BY 子句中出现了聚合函数(如 COUNT())导致的。在这种情况下,你需要将聚合函数移除 GROUP BY 子句,或者将它们放在一个子查询中。你可以尝试以下修改:
```
sql = """
SELECT date_z, SUM(cnt)
FROM (
SELECT id_person, date_z,
COUNT(*) + IFNULL(nn, 0) AS cnt
FROM (
SELECT id_person, date_z, date_finish
FROM luzhi_jilu_all
WHERE id_person = %s
AND (date_z = date_finish OR date_finish IS NULL)
) AS t1
LEFT JOIN (
SELECT id_person, date_finish, COUNT(*) AS nn
FROM luzhi_jilu_all
GROUP BY id_person, date_finish
) AS t2 ON t1.id_person = t2.id_person
AND t1.date_z = t2.date_finish
GROUP BY id_person, date_z
) AS t3
GROUP BY date_z
ORDER BY date_z DESC
"""
params = (id_person,)
cs.execute(sql, params)
jg = cs.fetchall()
```
在这个修改后的语句中,我们将 COUNT() 和 IFNULL() 函数放在了子查询中,并且在最终的外部查询中使用了 SUM() 函数来计算结果。同时,我们在子查询中也加入了 GROUP BY 子句以避免出现聚合函数在 GROUP BY 子句中的错误使用。
阅读全文