id | rem_id |max_val
-- | ------ |------
1 | 1 | 7
2 | 2 | 6
3 | 3 | 1
4 | 1 | 1
5 | 2 | 1
6 | 3 | 1
在上表中,我需要使用max_val列中的min val从rem_id列中删除重复项
id | rem_id |max_val
-- | ------ |------
1 | 1 | 7
2 | 2 | 6
3 | 3 | 1
答案 0 :(得分:1)
这将删除每个max_val
的最高价值rem_id
以及max_val
的倍数,删除id
更高的delete t
from t
left join t as i
on i.rem_id = t.rem_id
and (i.max_val > t.max_val
or (i.max_val = t.max_val and i.id < t.id)
)
where i.id is not null;
:
+----+--------+---------+
| id | rem_id | max_val |
+----+--------+---------+
| 1 | 1 | 7 |
| 2 | 2 | 6 |
| 3 | 3 | 1 |
+----+--------+---------+
rextester演示:http://rextester.com/QKIK17666
返回:
<meta name="viewport" content="width=device-width, initial-scale=1">