select id,channel,post from posts limit 10
+------+------------+-------+
| id | channel | post |
+------+------------+-------+
| 1433 | channel2 | 19353 |
| 1434 | channel2 | 19353 |
| 1435 | channel2 | 19354 |
| 1436 | channel2 | 19354 |
| 1437 | channel2 | 19356 |
| 1438 | channel2 | 19357 |
| 1439 | channel2 | 19358 |
| 1440 | channel2 | 19359 |
| 1441 | channel2 | 19360 |
| 1634 | channel2 | 19360 |
+------+------------+-------+
該表
id
表是primary key
,現在在該表中,我有一個重複的職位的頻道,我嘗試添加表並刪除所有重複的行與此查詢
ALTER ignore TABLE `posts` ADD UNIQUE key `unique_index` (`channel`, `post`);
但mysql 5.7.9
我們不能做到這一點!
,所以我想知道我怎樣才能刪除重複行並添加獨特的密鑰channel
,post
溶液中以套計算
DELETE FROM posts
WHERE ID Not in (SELECT*
FROM (SELECT MIN(ID)
FROM posts
GROUP BY channel, Post) B
)
我假設你想保留與最小的id記錄? –
@RaymondNijland:真的,現在並不重要,所以沒有什麼不同! – MrUnknow