英文:
How can i update a table using the output from a inner join query performed on 2 other tables?
问题
因此,上述两张表是我执行内连接并将这些值插入新表中的表格。"detected"表中的值通过人脸检测代码更新,而"students"表中的值是预先提供的。
insert into bscit3yr
select students.ROLLNO, students.SNAME, detected.TIMING, detected.RDATE
from detected
inner join students on detected.SNAME = students.SNAME
where students.DEPARTMENT = "BSCIT" and students.ACADEMICYEAR = "TY"
问题是我不想在新表"bscit3yr"中出现重复值,由于运行内连接查询多次,它会显示重复条目的错误。
mysql.connector.errors.IntegrityError: 1062 (23000): 重复条目'2021505',键'ROLLNO_UNIQUE'。
这是当内连接的输出已经存在于"bscit3yr"表中时我遇到的错误。
英文:
So the above two tables are the ones that I perform my inner join on and insert those values in a new table.
The values in the "detected" table gets updated through a face detection code, while the values in the table "students" is pre-fed
insert into bscit3yr
select students.ROLLNO, students.SNAME, detected.TIMING, detected.RDATE
from detected
inner join students on detected.SNAME = students.SNAME
where students.DEPARTMENT = "BSCIT" and students.ACADEMICYEAR = "TY"
The problem is I don't want duplicate values in my new table-"bscit3yr", since it runs the inner join query multiple times it shows me the error for duplicate entries
> mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry '2021505' for key 'ROLLNO_UNIQUE'.
This is the error that I get when the output of the inner join already exists in my "bscit3yr" table
答案1
得分: 0
在你的查询中,你可以删除已经存在于表格 bscit3yr
中的 ROLLNO
:
insert into bscit3yr
select students.ROLLNO, students.SNAME, detected.TIMING, detected.RDATE
from detected
inner join students on detected.SNAME = students.SNAME
where students.DEPARTMENT = "BSCIT" and students.ACADEMICYEAR = "TY"
and students.ROLLNO NOT IN (select ROLLNO from bscit3yr)
如果你想要更新已有的记录,你可以使用 REPLACE INTO
:
replace into bscit3yr
select students.ROLLNO, students.SNAME, detected.TIMING, detected.RDATE
from detected
inner join students on detected.SNAME = students.SNAME
where students.DEPARTMENT = "BSCIT" and students.ACADEMICYEAR = "TY"
REPLACE
的工作方式与 INSERT
相同,不同之处在于,如果表中的旧行具有与新行相同的主键或唯一索引的值,旧行将在插入新行之前被删除。
英文:
In your query you can eliminate ROLLNO
that are already in table bscit3yr
:
insert into bscit3yr
select students.ROLLNO, students.SNAME, detected.TIMING, detected.RDATE
from detected
inner join students on detected.SNAME = students.SNAME
where students.DEPARTMENT = "BSCIT" and students.ACADEMICYEAR = "TY"
and students.ROLLNO NOT IN (select ROLLNO from bscit3yr)
If you want to update the existing records you can use REPLACE INTO
:
replace into bscit3yr
select students.ROLLNO, students.SNAME, detected.TIMING, detected.RDATE
from detected
inner join students on detected.SNAME = students.SNAME
where students.DEPARTMENT = "BSCIT" and students.ACADEMICYEAR = "TY"
REPLACE
works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论