使用子查询MySQL删除

时间:2015-07-31 22:32:47

标签: mysql subquery

我正在尝试使用与'carretilla'相同的子查询来删除 这适用于SQL Server

DELETE FROM carretilla 
WHERE carcod IN (
  SELECT carcod  
  FROM carretilla 
  WHERE TIMESTAMPDIFF(MINUTE,carfch, NOW()) > 10 
  group BY carcod
);

我该怎么办? 感谢

1 个答案:

答案 0 :(得分:0)

AFAIK您不能引用您删除的表或直接更新,但如果您使用子查询,服务器将创建一个临时记录集并让您进行删除,因此这应该有效:

DELETE FROM carretilla 
WHERE carcod IN (
  SELECT carcod FROM (
    SELECT carcod  FROM carretilla 
    WHERE TIMESTAMPDIFF(MINUTE,carfch, NOW()) > 10 group BY carcod
  ) temp_table
);

请参阅此sample SQL Fiddle