[英]How to delete the row that has least value in a specific column if group by count of the column is greater than 1
我有一張如下表所示的名稱推薦
我想刪除所有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);
這是行不通的。
如果要使用二維,則必須使用 IN 子句。
你的子查詢沒有多大意義,所以你應該先測試一下,或者用想要的例子發布數據
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.