如果按列计数分组大于 1,如何删除特定列中具有最小值的行

How to delete the row that has least value in a specific column if group by count of the column is greater than 1

我有一个 table 像下面给出的名字推荐

我想删除所有cnt有最小值的行,并且存在多个ID_recipient的记录。

如果只有一条 ID_recipient 的记录,则无论 cnt 的值是多少,它都不应被删除。

蓝色突出显示的是必须保留的记录。

我试过了:

DELETE from table where( 
    SELECT DISTINCT(A.ID_recipient), DISTINCT(A.cnt) FROM (
          SELECT  MIN(cnt) as cnt FROM recomendation_table_ID_recipient GROUP BY ID_recipient HAVING COUNT(*) > 1 ) as A);

这是行不通的。

如果您想使用 2 个维度,则必须使用 IN 子句。

你的子查询没有多大意义,所以你应该先测试这个,或者 post 带有想要示例的数据

DELETE from recomendation_table_ID_recipient where (ID_recipient,cnt) IN ( 
    SELECT DISTINCT A.ID_recipient, A.cnt FROM (
          SELECT ID_recipient, MIN(cnt) as cnt FROM recomendation_table_ID_recipient GROUP BY ID_recipient HAVING COUNT(*) > 1 ) as A);
delete t1 from recomendation_table_ID_recipient t1 join (
    select ID_recipient, min(cnt) as cnt from recomendation_table_ID_recipient
    group by ID_recipient
    having count(*) > 1
) t2 on t1.ID_recipient = t2.ID_recipient and t1.cnt = t2.cnt;

See db-fiddle