首页 > 解决方案 > 如何根据两个表中的多个值删除一个表的值

问题描述

我有两张桌子:

Table 1 = products (id, name, organisationId, subcategoryId)
Table 2 = products_attribute_attributes (productId, attributeId)

我试图从表 2 中删除属性与特定 attributeId 匹配并与来自许多不同组织 ID 的产品相关的内容。

我尝试过的如下:

DELETE product_attributes_attribute FROM product_attributes_attribute
INNER JOIN product
ON  product.id = product_attributes_attribute.productId
WHERE attributeId = "154" AND organisationId IN (685,720,773,789,774,834,911,698);

MySQL给了我这样的回应:

错误代码:1175。您正在使用安全更新模式,并且您尝试更新没有使用 KEY 列的 WHERE 的表 要禁用安全模式,请切换 Preferences -> SQL Editor 中的选项并重新连接。

由于我不太了解 MySQL,我相当确定安全更新模式对于我保持启用以防止潜在的灾难性错误至关重要,因此我不打算禁用它。我不明白的是,当您看到代码中存在 WHERE 子句时,为什么它认为我没有使用 WHERE 子句。我究竟做错了什么?

标签: mysqlsql

解决方案


似乎您的表上没有主键,所以 mysql 引发错误

DELETE product_attributes_attribute 
FROM product_attributes_attribute
INNER JOIN product ON  product.id = product_attributes_attribute.productId
WHERE attributeId = "154" 
AND organisationId IN (685,720,773,789,774,834,911,698);

你应该或者改变你的表和广告主键

table product
id 

table products_attribute_attributes
productId, attributeId

或禁用 SQL_SAFE_UPDATES

SET SQL_SAFE_UPDATES = 0;

推荐阅读