上下文: 有车辆(yc_user_car),然后车辆有车检(yc_user_inspect)。现在车辆删除了,但是车检数据没有清理。导致错误。
第一次操作:
select id from yc_user_inspect where user_car_id not in (select id from yc_user_car);
delete from yc_user_inspect where id in (select id from yc_user_inspect where user_car_id not in (select id from yc_user_car));
如上操作,会报错

原因分析: 这里是mysql 不允许在筛选一张表的情况下,同时删除这张表。
解决方法:用子查询,子查询需要命名。如下
delete from yc_user_inspect where id in (select id from (select id from yc_user_inspect where user_car_id not in (select id from yc_user_car)) inspect);
如上, 最后的 inspect 是别名。
博客讨论了一种数据库操作问题,当尝试在MySQL中删除车辆记录时,未能同步清理对应的车检数据,导致错误。文章分析了错误原因,即在删除表的同时筛选该表。为解决此问题,提出了使用子查询和别名的方法来安全地删除车检记录。
174万+

被折叠的 条评论
为什么被折叠?



