我正在尝试删除96k记录。
此查询有效返回91k记录:
SELECT *
FROM xoops_bb_posts_text t
WHERE not exists (
select post_id
from xoops_bb_posts p
WHERE p.post_id = t.post_id
);
当我尝试删除这些记录时出现语法错误,但我没有看到它。
DELETE FROM xoops_bb_posts_text t
WHERE not exists (
select post_id
from xoops_bb_posts p
WHERE p.post_id = t.post_id
);
错误在哪里?
错误
SQL查询:文档
DELETE FROM xoops_bb_posts_text t
WHERE NOT EXISTS (
SELECT post_id
FROM xoops_bb_posts p
WHERE p.post_id = t.post_id
)
MySQL说:文档
#1064 - 您的SQL语法出错;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 在'WHERE not exists附近'(从xoops_bb_posts中选择post_id p WHERE p.post_id = t.post_'在第2行
答案 0 :(得分:10)
如果在删除调用中对别名进行别名,则必须使用别名作为参数:
DELETE alias FROM tablerealname as ALIAS ...
所以在OP的原始问题中,他只需在DELETE之后添加别名:
DELETE t FROM xoops_bb_posts_text as t WHERE NOT EXISTS (
SELECT post_id
FROM xoops_bb_posts as p
WHERE p.post_id = t..post_id
)
答案 1 :(得分:7)
对我来说,通过使用带有delete
的{{1}}语句并查找不匹配的行,可以更轻松地解决此问题。像这样:
outer join
或只是更改您的查询:
delete t from xoops_bb_posts_text as t
left outer join xoops_bb_posts as p
on p.post_id = t.post_id
where p.post_id is null;
答案 2 :(得分:1)
您不能在单表DELETE in MySql中对表名进行别名。您需要使用完整的表名,如下所示:
DELETE FROM xoops_bb_posts_text
WHERE not exists (select post_id from xoops_bb_posts p WHERE p.post_id = xoops_bb_posts_text.post_id );
答案 3 :(得分:0)
您无法在delete语句中指定表别名。
答案 4 :(得分:0)
来自我的评论:
DELETE FROM xoops_bb_posts_text t
WHERE NOT EXISTS (
SELECT *
FROM xoops_bb_posts p
WHERE p.post_id = t.post_id
);
答案 5 :(得分:-1)
CREATE TABLE IF NOT EXISTS `tbl_student1` (
`stud_id` int(11) NOT NULL AUTO_INCREMENT,
`stud_name` varchar(255) CHARACTER SET utf8 NOT NULL,
`stream_id` int(11) NOT NULL,
`address` varchar(255) CHARACTER SET utf8 NOT NULL,
`s_date` datetime NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`stud_id`),
FOREIGN KEY ('stream_id')
REFERENCES tbl_stream ('stream_id')
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;
答案 6 :(得分:-2)
我不知道语法“EXISTS”。我认为最好使用“in”和“not in”
尝试:
DELETE FROM xoops_bb_posts_text WHERE post_id not in (SELECT post_id FROM xoops_bb_posts)