简体   繁体   English

MySQL 删除查询未按预期工作

[英]MySQL delete query is not working as expected

I am running a query directly in MySQL Workbench.我直接在 MySQL Workbench 中运行查询。 It is a work in progress, but I can't get past this issue.这是一项正在进行的工作,但我无法解决这个问题。

The query is as follows:查询如下:

#Temporary table to hold dcsIds that are past grace period
CREATE TABLE pastGracePeriod (
    dcsId varchar(50) NOT NULL
);

INSERT INTO pastGracePeriod
SELECT dcsId FROM loyaltyOptOutGracePeriod
WHERE optOutDate <= ADDDATE(CURRENT_TIMESTAMP(), INTERVAL -15 DAY);

#temporary table to hold dcsIds that are validated against subscriptions table
CREATE TABLE validatedDcsIds (
    dcsId varchar(50) NOT NULL
);

INSERT INTO validatedDcsIds
SELECT subscriptions.dcsId 
FROM subscriptions
INNER JOIN pastGracePeriod ON subscriptions.dcsId = pastGracePeriod.dcsId
WHERE subscriptions.optOutDate <= ADDDATE(CURRENT_TIMESTAMP(), INTERVAL -15 DAY)
    AND subscriptions.subscriptionId IN (24,25,30)
    AND subscriptions.optInStatus='N';

#delete items in validatedDcsIds table from externalId table
DELETE FROM externalId
WHERE EXISTS (SELECT dcsId FROM validatedDcsIds
    WHERE externalId.dcsId = validatedDcsIds.dcsId);

DROP TABLE pastGracePeriod;
DROP TABLE validatedDcsIds;

The query takes dcsIds (PK) from a table called loyaltyOptOutGracePeriod , validates them against another table called subscriptions , then takes those validated dcsIds and deletes them from a table called externalId .该查询从名为loyaltyOptOutGracePeriod的表中获取dcsIds (PK),根据另一个名为subscriptions的表对其进行验证,然后获取那些经过验证的dcsIds并将它们从名为externalId的表中删除。

The query works fine until it gets to the DELETE clause.在到达DELETE子句之前,查询工作正常。 In the externalId table, the PK is a composite key consisting of fields dcsId , appId , and appName .externalId表中,PK 是由字段dcsIdappIdappName组成的组合键。 As it is written above, I get Error 1175: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column .如上所述,我得到Error 1175: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

If I change it to:如果我将其更改为:

DELETE FROM externalId
WHERE EXISTS (SELECT dcsId FROM validatedDcsIds
    WHERE externalId_backup.dcsId = validatedDcsIds.dcsId)
AND appId <> NULL;

I get no errors, but nothing gets deleted from the externalId table as expected.我没有收到任何错误,但没有按预期从externalId表中删除任何内容。

I am at a loss, any suggestions are very welcome!我不知所措,非常欢迎任何建议!

NOTE: I do not need to know how to disable safe update mode.注意:我不需要知道如何禁用安全更新模式。

Do not use WHERE EXISTS.不要使用 WHERE EXISTS。 Use Multiple-Table Syntax :使用多表语法

DELETE table1
FROM table1
JOIN table2 ON {joining conditions}
WHERE {additional conditions}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM