如何在mysql数据库中删除此表中的行,删除后我应该在列valeurs中有4个不同的值。
我的数据库的实际状态:
+----+-----------+---------+---------------------+
| id | action_id | valeurs | temps_action |
+----+-----------+---------+---------------------+
| 81 | 1 | ON_1 | 2016-04-26 11:14:43 |
| 80 | 2 | OFF_2 | 2016-04-26 11:14:41 |
| 84 | 2 | OFF_2 | 2016-04-26 11:14:48 |
| 83 | 1 | ON_1 | 2016-04-26 11:14:46 |
| 79 | 1 | OFF_1 | 2016-04-26 11:14:40 |
| 78 | 2 | ON_2 | 2016-04-26 11:14:38 |
| 77 | 1 | ON_1 | 2016-04-26 11:14:35 |
| 82 | 2 | OFF_2 | 2016-04-26 11:14:45 |
+----+-----------+---------+---------------------+
我想最终:
+----+-----------+---------+---------------------+
| id | action_id | valeurs | temps_action |
+----+-----------+---------+---------------------+
| 81 | 1 | ON_1 | 2016-04-26 11:14:43 |
| 80 | 2 | OFF_2 | 2016-04-26 11:14:41 |
| 79 | 1 | OFF_1 | 2016-04-26 11:14:40 |
| 78 | 2 | ON_2 | 2016-04-26 11:14:38 |
+----+-----------+---------+---------------------+
答案 0 :(得分:1)
你可以这样使用
delete from my_table as t
where (t.id, t.valeurs) not in ( select max(t2.id), t2.valeurs
from my_table as t2
group by valeurs);
答案 1 :(得分:1)
这样的查询为每个action_id,valeurs
保存了最多temps_action的行delete
from my_table
where (action_id, temps_action, valeurs) not in
(select * from (select action_id, max(temps_action), valeurs
from my_table
group by action_id, valeurs) t1);
答案 2 :(得分:0)
您真正想要做的是删除重复的行。这是一个可以帮助您的示例查询:
DELETE FROM TableName
WHERE valeurs IN (SELECT *
FROM (SELECT valeurs FROM TableName
GROUP BY valeurs
HAVING (COUNT(*) > 1) AS Something
)
);