以下语句中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() 出现以下错误pymysql.err.InternalError: (1052, "Column 'id_person' in field list is ambiguous"),请检查并修改
时间: 2024-02-10 07:16:13 浏览: 72
这个错误是因为在 SQL 语句中 id_person 列在多个表中出现,导致无法确定具体是哪个表的列。你需要在 SQL 语句中明确指定使用哪个表的 id_person 列,可以使用别名来解决这个问题。例如,将 id_person 改为 t1.id_person 或 t2.id_person,表示使用 t1 或 t2 表中的 id_person 列。修改后的 SQL 语句如下:
```
SELECT date_z, SUM(cnt)
FROM (
SELECT t1.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 t1.id_person, date_z
) AS t3
GROUP BY date_z
ORDER BY date_z DESC
```
你可以将代码中的 SQL 语句替换为修改后的语句,然后重新运行程序,该错误应该就会消失了。
阅读全文