mysql - 如何根据两个表中的多个值删除一个表的值
问题描述
我有两张桌子:
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 子句。我究竟做错了什么?
解决方案
似乎您的表上没有主键,所以 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;
推荐阅读
- swift - Laravel Swift 添加 UUID
- javascript - Webpack:抑制来自特定文件的警告
- nlog - NLog 文件目标 - 为 Splunk Forwarder 记录日志文件并避免锁定的最佳性能?
- swift - 如何取消接收器中的合并订阅?
- javascript - 在 JS 数组中创建对象
- powershell - PowerShell Remoting BinaryFormatter 序列化
- angular - 模糊事件函数后如何验证?
- c++ - 可以在循环中使用 C++ STL 的这个函数 queue::size() 影响行为吗
- html - 背景图像不起作用,路径正确
- python - 使用 tkinter 从浏览按钮获取全局变量中的文件路径