首页 > 解决方案 > 使用内部连接和 where 条件更新

问题描述

我正在研究 MySQL 并创建了两张表,一张用于students_data,一张用于Attendance_details

Attendance table有学生 ID 的 FK 这是Attendance table

AttID, ID, DateTime, Statues

还有students_data桌子

fname, lname, ID
ABN   , AA , 123

我的问题:

我想Attendance_details根据学生 ID 更新表格,如下面的查询

sql = " UPDATE Attendance_table INNER JOIN students_data ON Attendance_table.ID=students_data.ID SET Attendance_table.ID=students_data.ID , Statues=%s , DateTime=%s , WHERE students_data.fname = %s"
val=(1,formatted_date,name)
mycursor.execute(sql,val)
mydb.commit()

我有一个错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE students_data.fname = 'ABN''

注意:我正在从事人脸识别项目,其中所有人脸都有一个预定义的名称,并将其与人脸进行比较,students_data 因此名称 ABN 是从人脸中读取的 :)

任何帮助或建议!

标签: pythonmysqlsql-update

解决方案


你在 where 之前使用了一个额外的逗号

DateTime=%s , WHERE students_data.fname = %s"

去掉这个逗号。您的代码应该是:

sql = " UPDATE Attendance_table INNER JOIN students_data ON Attendance_table.ID=students_data.ID SET Attendance_table.ID=students_data.ID , Statues=%s , DateTime=%s  WHERE students_data.fname = %s"

推荐阅读