你可以使用内连接查询在两个其他表上执行后的输出来更新表。

huangapple go评论56阅读模式
英文:

How can i update a table using the output from a inner join query performed on 2 other tables?

问题

第1张表

第2张表

因此,上述两张表是我执行内连接并将这些值插入新表中的表格。"detected"表中的值通过人脸检测代码更新,而"students"表中的值是预先提供的。

用于"students"表的DDL

用于"detected"表的DDL

用于"tybscit3yr"表的DDL

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"表中时我遇到的错误。

英文:

1ST table

2ND table

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

DDL FOR TABLE 'students'

DDL FOR TABLE 'detected'

DDL FOR TABLE 'tybscit3yr'

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.

huangapple
  • 本文由 发表于 2023年3月31日 17:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定