MYSQL中子查询同时执行DELETE或UPDATE操作的简单方法
DELETE FROM `sites_cates` WHERE sid IN (
SELECT `a`.`sid`
FROM `sites_cates` AS a
LEFT JOIN `sites` AS b ON `a`.`site_id` = `b`.`sid`
WHERE `b`.`name` IS NULL
);
这样一条语句会被报错:
#1093 - You can't specify target table 'sites_cates' for update in FROM clause
网上流传最多的解决办法:http://yueliangdao0608.blog.51cto.com/397025/81390
实际上可以更简单:
DELETE FROM `sites_cates` WHERE sid IN (
SELECT `a`.`sid`
FROM (
SELECT *
FROM `sites_cates`
) AS a
LEFT JOIN `sites` AS b ON `a`.`site_id` = `b`.`sid`
WHERE `b`.`name` IS NULL
);
游客 (未验证)
周六, 2009/01/31 - 16:52
Permalink
看不出简单在哪里
看不出简单在哪里
zhangsp
周一, 2009/02/09 - 18:49
Permalink
DELETE a.* FROM
DELETE a.* FROM `sites_cates` AS a LEFT JOIN `sites` AS b ON `a`.`site_id` = `b`.`sid` WHERE `b`.`name` IS NULL 这样不就简单了,mysql与oracle的不同点是mysql不能在从句中使用主句使用的表,但是oracle是可以的,这是mysql和oracle的sql的不同点之一
zhangsp
周一, 2009/02/09 - 18:50
Permalink
DELETE a.* FROM
DELETE a.* FROM `sites_cates` AS a LEFT JOIN `sites` AS b ON `a`.`site_id` = `b`.`sid` WHERE `b`.`name` IS NULL 这样不就简单了,mysql与oracle的不同点是mysql不能在从句中使用主句使用的表,但是oracle是可以的,这是mysql和oracle的sql的不同点之一