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 can't 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)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值