gpt4 book ai didi

mysql - 删除表中与另一个表中的行不匹配的行

转载 作者:行者123 更新时间:2023-11-29 06:25:40 25 4
gpt4 key购买 nike

我知道这个问题之前已经被问过,但是给出的解决方案假设了 id(单列)关系。我尝试过适应,但给定的解决方案不适用于我的情况。

更新:我一定是把头抬得太高了,以至于我转过身来。查看文档和其他代码,我发现我的问题和意图应该是“删除表中与另一个表中的行匹配的行”。这个问题更容易回答,而且我已经解决了。

我有 2 个 MySQL INNODB 表 - clasclas_import - 它们有一些共同的列。我正在将 CSV 导入到 clas_import 中,并且想要删除 clasclas_import 中不存在的任何行。

clas_import 定义如下:

+-----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| term | smallint(4) unsigned | NO | | NULL | |
| subject | varchar(10) | NO | | 0 | |
| catalog | smallint(5) unsigned | NO | | 0 | |
| component | varchar(10) | NO | | 0 | |
| section | varchar(5) | NO | | 0 | |
+-----------+----------------------+------+-----+---------+----------------+

clas 有更多列,但 idterm 等列的定义与上面相同。

唯一的“类”由term + subject + catalog + component +的组合定义部分。因此,我想找到 clas 中与 clas_import 中的条目不匹配的所有条目,并将其删除。额外的好处是,并非所有行都在进行比较 - 只有那些term行与某些值匹配的行

示例数据:

class_import

+----+------+---------+---------+-----------+---------+
| id | term | subject | catalog | component | section |
+----+------+---------+---------+-----------+---------+
| 1 | 1730 | ENG | 102 | LEC | 1A04 |
| 15 | 1730 | BLA | 102 | LEC | 1A04 |
+----+------+---------+---------+-----------+---------+

clas(列被截断以仅显示相关数据)

+----+------+---------+---------+-----------+---------+
| id | term | subject | catalog | component | section |
+----+------+---------+---------+-----------+---------+
| 23 | 1730 | ENG | 102 | LEC | 1A04 |
| 26 | 1730 | BLA | 102 | LEC | 1A04 |
| 30 | 1730 | ENG | 100 | LEC | 1A04 |
| 11 | 1700 | ENG | 102 | LEC | 1A04 |
+----+------+---------+---------+-----------+---------+

期望的结果:我可以运行查询并删除第 23 行和第 26 行,同时保持第 30 行和第 11 行不变。

我的查询:

DELETE
FROM `clas`
WHERE
`clas`.`term` IN (1730) AND
NOT EXISTS(
SELECT
1
FROM
`clas_import` as `ci`
WHERE
`clas`.`term` = `ci`.`term` AND
`clas`.`subject` = `ci`.`subject` AND
`clas`.`catalog` = `ci`.`catalog` AND
`clas`.`component` = `ci`.`component` AND
`clas`.`section` = `ci`.`section`
)

该查询不会删除任何内容。当我将 DELETE 更改为 SELECT * 时,不会返回任何行。两个表都已填充,如果我对termsubject等进行简单的查询匹配,我会得到返回的行 - 所以我知道数据是好的。

哪里出了问题?

编辑这是一个dbfiddle:https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=6cfd81899fbacb5e567ec84d6e5ed7d6

最佳答案

Your query works ,还有其他事情发生。我的第一个猜测是检查空格差异。

但我在写完答案后才意识到,所以这就是你如何做得更干净的方法。

<小时/>

你想要一个left excluding join仅匹配 clas 中与 clas_import 中不匹配的行。

enter image description here

delete clas
from clas
left join clas_import ci on
clas.term = ci.term AND
clas.subject = ci.subject AND
clas.catalog = ci.catalog AND
clas.component = ci.component AND
clas.section = ci.section
-- This bit makes it exclusive.
where ci.id is null and
-- And ignore other terms.
clas.term = 1730;

dbfiddle

关于mysql - 删除表中与另一个表中的行不匹配的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59620536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com