MySQL重复数据中限定操作n条

对于一个表,有时可能里面有很多重复的条,比如:

+-----------+---------+
| coupon_id | user_id |
+-----------+---------+
| 8 | 15 |
| 5 | 15 |
| 8 | 15 |
| 6 | 15 |
| 5 | 15 |
| 8 | 15 |
| 6 | 15 |
| 10 | 15 |
| 10 | 15 |
| 10 | 15 |
| 11 | 15 |
| 12 | 15 |
| 13 | 15 |
+-----------+---------+

这时如果要根据coupon_id和user_id仅删除一条数据,比如15,10,那么怎么办呢?

解决:

  使用 limit n限制

eg:

delete from coupon_user where coupon_id=#{couponId} and user_id =#{userId} limit 1   ------限制每次删除1条
在语句末尾添加 limit n即可

原文地址:https://www.cnblogs.com/XT-xutao/p/10847257.html