[英]From every set of rows with the same value in column A delete all rows but the row with the highest value in column B
I'm searching for a SQL command that does the following: 我正在搜索执行以下操作的SQL命令:
From every set of rows with the same value in column A delete all rows but the row with the highest value in column B. If there are multiple rows with the same B value in a set keep at least one of them. 从列A中具有相同值的每组行中删除除了列B中具有最高值的行之外的所有行。如果在集合中存在具有相同B值的多个行,则保留其中至少一个。
Initial state: 初始状态:
+---+---+
| A | B |
+---+---+
| x | 1 |
| x | 2 |
| y | 3 |
+---+---+
Desired result: 期望的结果:
+---+---+
| A | B |
+---+---+
| x | 2 |
| y | 3 |
+---+---+
Oy, this is a pain. 哦,这是一种痛苦。 I think the best way is truncate
/ insert
: 我认为最好的方法是truncate
/ insert
:
create table temp_t as
select *
from t;
truncate table t;
insert into t(a, b)
select distinct a, b
from temp_t tt
where tt.b = (select max(tt2.b) from temp_t tt2 where tt2.a = tt.a);
Another alternative would be to add a third column and assign that a unique number, which can then be used for the delete. 另一种方法是添加第三列并分配一个唯一的编号,然后可以将其用于删除。
This query should work!! 这个查询应该工作!!
I am retaining all the MAX B's for each A and deleting all the remaining rows from the table which are not required. 我保留每个A的所有MAX B并删除表中不需要的所有剩余行。
DELETE s1
FROM table s1,
( SELECT A,MAX(B) AS B FROM table GROUP BY A ) s2
WHERE s1.A = s2.A
AND s1.B <> s2.B;
Use MySQL's multi-table delete: 使用MySQL的多表删除:
delete t2
from mytable t1, mytable t2
where t2.A = t1.A
and t2.B < t1.B
An index on column A
will make this perform well. A
列上的索引将使其表现良好。
Use a JOIN
with a subquery that gets the maximum B for each A, and make the JOIN
condition match the rows where B
doesn't match this. 使用带有子查询的JOIN
,该子查询为每个A获取最大B,并使JOIN
条件与B
不匹配的行匹配。
DELETE t1
FROM Table AS t1
JOIN (
SELECT A, MAX(B) AS maxB
FROM Table
GROUP BY A) AS t2
ON t1.A = t2.A AND t1.B != maxB
To get rid of the duplicates that remain, use one of the solutions in How to delete duplicates on a MySQL table? 要删除剩余的重复项,请使用如何删除MySQL表上的重复项中的一个解决方案?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.