mysql返回后删除,在mysql中,删除后可以返回删除的行吗?

How to return deleted records of following query in mysql?

DELETE t1

FROM t1

LEFT JOIN t2 on (t1.t2_id = t2.id)

WHERE t2.id IS NULL OR t2.is_valid = false

Background:

$ mysql --version

mysql Ver 14.14 Distrib 5.6.23, for osx10.8 (x86_64) using EditLine wrapper

解决方案

MySQL doesn't have the equivalent of the output or returning clauses provided by other databases. Your best bet is a temporary table:

CREATE TABLE TheDeletedIds as

SELECT t1.id

FROM t1 LEFT JOIN

t2

ON t1.t2_id = t2.id

WHERE t2.id IS NULL OR t2.is_valid = false;

DELETE t1

FROM t1

WHERE t1.id IN (SELECT id FROM TheDeletedIds);

Then the table you just created has the ids you want.

Note: It is important to use the newly-created table for the deletion. Otherwise, another thread/process could change the data between the time you capture the ids and the time you delete them.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值