基于唯一ID和非唯一ID从mysql中删除多个记录

时间:2013-11-01 23:27:54

标签: mysql sql sql-delete records

我是MySQL新手,在转换SQL语句时遇到问题,如果可能的话,我希望得到一些帮助。

故障是我需要能够根据上次登录日期从一个表中删除项目,遗憾的是我无法控制表设计(这是有缺陷的)并且必须将characterID与characterUID和LastLogin进行比较date,以便从另一个只删除charcaterID(非唯一)对象的表中删除项目。

这是我到目前为止所拥有的

DELETE FROM object_data WHERE CharacterID IN (
    SELECT CharacterID FROM object_data 
    WHERE CharacterID IN (
        SELECT CharacterID FROM character_data
            WHERE LastLogin < DATE_SUB(NOW(), INTERVAL 30 DAY)
            AND PlayerUID NOT IN (
                SELECT PlayerUID FROM character_data 
                    WHERE LastLogin > DATE_SUB(NOW(), INTERVAL 30 DAY))))

我得到的错误是1093.你不能从FROM子句中的update更新目标表'object_date'。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

立即尝试:

DELETE FROM object_data WHERE CharacterID IN (
    SELECT CharacterID FROM  (
        SELECT CharacterID FROM character_data
            WHERE LastLogin < DATE_SUB(NOW(), INTERVAL 30 DAY)
            AND PlayerUID NOT IN (
                SELECT PlayerUID FROM character_data 
                    WHERE LastLogin > DATE_SUB(NOW(), INTERVAL 30 DAY)))y)

答案 1 :(得分:0)

如果您没有为子查询表名称命名

,有时会使用SQL barfs
DELETE FROM object_data 
WHERE CharacterID IN 
(
  SELECT CharacterID 
  FROM object_data 
  WHERE CharacterID IN 
  (
    SELECT CharacterID 
    FROM character_data
    WHERE LastLogin < DATE_SUB(NOW(), INTERVAL 30 DAY)
    AND PlayerUID NOT IN 
    (
       SELECT PlayerUID 
       FROM character_data 
       WHERE LastLogin > DATE_SUB(NOW(), INTERVAL 30 DAY)  
    ) AS t1
  ) AS t2
) AS t3