[英]delete row with lower value only if there are more than n rows
I want to delete the row with lower value only if there are more than 5 rows in the table. 我想用较低的值删除该行仅当有超过5个表中的行。
In this example I want to delete the row with id = 4 (value is lower and there are more than 5 rows): 在此示例中,我要删除id = 4(值较低且有5行以上)的行:
|--------------------|
| id | value |
|--------------------|
| 1 20 |
|--------------------|
| 2 15 |
|--------------------|
| 3 30 |
|--------------------|
| 4 5 |
|--------------------|
| 5 50 |
|--------------------|
| 6 10 |
|--------------------|
I wonder if it is possible with one query. 我不知道一个查询是否可能。
You can use a left join
of the same table 您可以使用同一表的left join
delete t
from your_table t
left join
(
select id
from your_table
order by value desc
limit 5
) tmp on t.id = tmp.id
where tmp.id is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.