MYSQL存储过程删除

时间:2014-05-01 13:37:43

标签: mysql sql stored-procedures

我有存储过程:

BEGIN
SELECT @id_for_del := ful.social_id
FROM facebook_users_likes ful 
where
(select count(*) from facebook_users fu where fu.social_id = ful.social_id) = 0;
IF NOT @id_for_del IS NULL
THEN 
DELETE from facebook_users_likes WHERE social_id = @id_for_del;
END IF;
END

选择会返回46 000行,但每个执行存储过程只删除一行。 那么,如何一次删除所有46 000行?

1 个答案:

答案 0 :(得分:1)

以下是selectdelete

的直接翻译
DELETE FROM facebook_users_likes ful 
WHERE (select count(*) from facebook_users fu where fu.social_id = ful.social_id) = 0;

在此上下文中使用not exists更有效:

DELETE FROM facebook_users_likes ful 
WHERE NOT EXISTS (select 1 from facebook_users fu where fu.social_id = ful.social_id);

如果您在facebook_users(social_id)上有索引,则尤其如此。