从MYSQL子查询中删除

时间:2016-04-18 04:21:22

标签: mysql sql mysql-workbench

我想删除与子查询匹配的表中的所有记录,如下所示:

delete from device where clientMac =   (select * from device  where clientMac = '80:d6:05:02:1d:b9');

我收到了这个错误:

  

错误代码1241:操作数应包含1列

我也想避免:

  

错误1093:您无法在FROM子句

中指定要更新的目标表

5 个答案:

答案 0 :(得分:3)

你必须使用IN

DELETE FROM device WHERE clientMac IN (SELECT clientMac FROM device where clientMac='80:d6:05:02:1d:b9')

答案 1 :(得分:2)

如果我理解您的查询,那么您的查询不需要子查询。请看这个:

DELETE FROM device WHERE clientMac IN(SELECT clientMac FROM device where clientMac = '80:d6:05:02:1d:b9')

等同于:

DELETE FROM device WHERE clientMac = '80:d6:05:02:1d:b9';

我认为你想要实现别的目标。请确认。

答案 2 :(得分:1)

错误是由于列不匹配造成的。您的选择查询必须只返回 clientMac 列,如果预期有多个结果,则使用IN Query,如下所示

delete from device where clientMac IN (select clientMac from device where clientMac = '80:d6:05:02:1d:b9');

答案 3 :(得分:1)

delete from device 
where clientMac in (
      select * from 
      (select clientMac  from device where clientMac = '80:d6:05:02:1d:b9')as t
    );

答案 4 :(得分:1)

这里id字段是表的主键。

delete 
from device 
where id in (select * 
             from (select id 
                   from device 
                   where FIND_IN_SET(clientMac,'80:d6:05:02:1d:b9')) 
             as t1 )