mysql查找并删除记录_mysql 子查询删除记录
mysql 子查询删除记录
mysql 可以通过子查询得到要删除的记录条件,然后通过子查询得结果匹配要删除的记录。但是 mysql 不能直接删除子查询表中的数据,必须通过一个临时表来解决。例如:
delete from t_transaction where exists
(select d.* from t_ti_category a,t_category b,t_trade_item c,t_transaction d
where b.FID=a.FCategory and a.FTradeItem=c.FID and c.FTrans=d.FID and b.FID=2549 and t_transaction.FID=d.FID)
在该 sql 语句中由于子查询中包含 t_transaction 表,但我们同时要删除 t_transaction 表中的记录,所以允许该语句时报如下错误:
1093-You cant specify target table t_transaction for update in from clause.
修改后的操作语句如下:
delete from t_transaction where exists
(select e.FID from
(select d.* from t_ti_category a,t_category b,t_trade_item c,t_transaction d
where b.FID=a.FCategory and a.FTradeItem=c.FID and c.FTrans=d.FID and b.FID=2549) e
where t_transaction.FID=e.FID)
mysql 子查询删除记录 mysql 可以通过子查询得到要删除的记录条件,然后通过子查询得结果匹配要删除的记录。但是 mysql 不能直接删除子查询表中的数据,必须通过一个临时表来解决。例如: delete from t_transaction where exists (select d.* from t_ti_category a,t_category b,t_trade_item c,t_transaction d where b.FID=a.FCategory and a.FTradeItem=c.FID and c.FTrans=d.FID and b.FID=2549 and t_transaction.FID=d.FID) 在该 sql 语句中由于子查询中包含 t_transaction 表,但我们同时要删除 t_transaction 表中的记录,所以允许该语句时报如下错误: 1093-You cant specify target table t_transaction for update in from clause. 修改后的操作语句如下: delete from t_transaction where exists (select e.FID from (select d.* from t_ti_category a,t_category b,t_trade_item c,t_transaction d where b.FID=a.FCategory and a.FTradeItem=c.FID and c.FTrans=d.FID and b.FID=2549) e where t_transaction.FID=e.FID)下一篇:
单例与有上限多例模式