gpt4 book ai didi

mysql - 如何删除未知号码的最后一条记录(有条件)?

转载 作者:行者123 更新时间:2023-11-29 07:50:07 29 4
gpt4 key购买 nike

您能告诉我如何删除未知号码的最后一条记录(有条件)吗?

例如,在这种情况下我想删除id为6到10的记录。

注意:该表和记录不是恒定的。

+----+-----+---------+
| id | url | emailid |
+----+-----+---------+
| 1 | 10 | 1 |
| 2 | 20 | 0 |
| 3 | 30 | 2 |
| 4 | 40 | 0 |
| 5 | 50 | 10 |
| 6 | 60 | 0 |
| 7 | 70 | 0 |
| 8 | 80 | 0 |
| 9 | 90 | 0 |
| 10 | 100 | 0 |
+----+-----+---------+

谢谢...

最佳答案

您似乎想要删除最后一组所有值为0的记录。这有点痛苦。您可以找到最小的 id,如下所示:

select min(t.id)
from table t
where t.emailid = 0 and
not exists (select 1 from table t2 where t2.id > t.id and t2.emailid <> 0);

逻辑是:查找emailid为0且后续emailid不为零的所有行。

您可以使用join将其放入delete中:

delete t
from table t cross join
(select min(t.id) as first0id
from table t
where t.emailid = 0 and
not exists (select 1 from table t2 where t2.id > t.id and t2.emailid <> 0)
) tmin
where t.id >= tmin.first0id;

关于mysql - 如何删除未知号码的最后一条记录(有条件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26714085/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com