繁体   English   中英

MySQL通过一个查询从2个表中删除

[英]MySQL delete from 2 tables with one query

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

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

表:objects_entity guid:整数标题:文本

objects_entity中的guid是entities.guid的外键

我需要删除基于子类型= 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

这超出了我,因为与上面相同的select语句正常工作....表存在。

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

你需要更换

DELETE entities, objects_entity

DELETE a, b

因为你用ab对表进行别名。

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM