如果在MySQL中找到第二行,则将其删除

时间:2019-05-28 11:11:48

标签: mysql

如果商人有2行,我试图在MySql中删除一行。删除哪一行都没有关系。

当我跑步时:

SELECT * FROM products.merchant_configs WHERE `configKey` = 'CHECK_BALANCE_SUBJECT'
AND merchantId = 6;

我知道:

enter image description here

现在,我尝试删除以下行之一:

DELETE FROM products.merchant_configs
WHERE id = (
    SELECT id FROM products.merchant_configs WHERE `configKey` = 'CHECK_BALANCE_SUBJECT'
    AND merchantId = 6
    LIMIT 2 OFFSET 1
);

我收到错误:

Error Code: 1093. You can't specify target table 'merchant_configs' for update in FROM clause

这是怎么回事?

1 个答案:

答案 0 :(得分:0)

将查询嵌套到另一个select语句中

DELETE FROM products.merchant_configs
WHERE id = (SELECT t.id FROM (
    SELECT id FROM products.merchant_configs WHERE `configKey` = 'CHECK_BALANCE_SUBJECT'
    AND merchantId = 6
    LIMIT 2 OFFSET 1
) t);

请参见demo