【问题标题】:MySQL delete from 2 tables with one queryMySQL 从 2 个表中删除一个查询
【发布时间】:2012-07-28 11:17:25
【问题描述】:

我需要根据一个表中的查询从两个表中删除

表:实体 指导:整数 子类型:整数 time_created:整数(Unix 时间戳)

表:objects_entity 指导:整数 标题:文字

objects_entity 中的guid 是entities.guid 的外键

我需要根据 subtype=17 删除两个表中的相关记录,并且 time_created 在实体中超过 14 天(因此还要删除相关 objects_entity)

我非常不擅长 SQL,通过查看我创建的示例:

DELETE entities, objects_entity FROM entities a INNER JOIN objects_entity b on b.guid = a.guid AND a.subtype =17 AND a.time_created < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 14 DAY))

但这给出了错误:

#1109 - Unknown table 'entities' in MULTI DELETE

这超出了我的范围,因为与上面相同的选择语句可以正常工作....该表存在。

任何想法我的语法有什么问题?非常感谢。

【问题讨论】:

标签: mysql sql


【解决方案1】:

你需要更换

DELETE entities, objects_entity

DELETE a, b

因为您使用 ab 为表命名。

【讨论】:

    【解决方案2】:
    DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    tbl_name[.*] [, tbl_name[.*]] ...
    FROM table_references
    [WHERE where_condition]
    

    DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    FROM tbl_name[.*] [, tbl_name[.*]] ...
    USING table_references
    [WHERE where_condition]
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2014-11-15
      相关资源
      最近更新 更多