English 中文(简体)
从数据库中删除附有条件的重复条目
原标题:Remove duplicate entries from database with conditions
  • 时间:2010-09-16 02:42:02
  •  标签:
  • sql
  • mysql

我怀着良好的眼光,但为了找到一种解决办法,希望有人能够帮助。

我拥有一个记录检查例行结果的内部伐木申请结果的MySQL表格,有几部检查例行记录,在跟踪器栏中确定:

id (int)(PK), tracker (int), time (timestamp), result (int)

只有在以前的结果不相同的情况下,才需要记录结果,只需要反映变化。 令人不舒服的是,当一个月前建造(在磨难中)时,这种情况就被忽视了,结果被盲目记录,没有检查以往的结果。 现在的记录已经记录下来,但我仍然剩下几千个牢房,其中很多是重复条目,而我是在为离开变化点而清除这些条目之后的。

因此,我需要逐行走,看一看这一轨迹者以往所记录的结果,删除行文(如果是这样的话),这比我对我的我的我的后勤工作经历更远,而且我迄今所作出的努力相当贫穷!

谁能帮助?

最佳回答

使用:

   DELETE a
     FROM YOUR_TABLE a
LEFT JOIN (SELECT MAX(t.id) AS latest_id
             FROM YOUR_TABLE t
         GROUP BY t.tracker, t.result) b ON b.latest_id = a.id
    WHERE b.latest_id IS NULL

候补成员:

DELETE FROM YOUR_TABLE
 WHERE id NOT IN (SELECT x.latest_id
                   FROM (SELECT MAX(t.id) AS latest_id
                          FROM YOUR_TABLE t
                      GROUP BY t.tracker, t.result) x )
问题回答

有人抱怨说,执行速度缓慢,但这可能对你产生影响。 这肯定比你所能做的事情更快:

select DISTINCT id, tracker, time, result
from table;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签