0

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

失败是我需要能够根据上次登录日期从一个表中删除项目,不幸的是我无法控制表设计(这是有缺陷的)并且必须将 characterID 与 characterUID 和 LastLogin 日期进行比较从仅按 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 子句中从更新中指定目标表 'object_date'。

有任何想法吗?

4

2 回答 2

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
于 2013-11-01T23:38:56.900 回答
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)
于 2013-11-01T23:35:44.407 回答