简体   繁体   English

mysql 使用特殊 id 和日期字段从查询中删除

[英]mysql delete from query using special id and date field

What i have is two columns specialid and date in tblSpecialTable, my table has duplicate specialID's, i want to delete from the table where date column is the older date and where specialid's are duplicated.我拥有的是 tblSpecialTable 中的两列 specialid 和 date,我的表有重复的 specialID,我想从表中删除日期列是较旧的日期并且 specialid 重复的表。

See my example:看我的例子:

mysql> SELECT * FROM test;
+------+---------------------+
| id   | d                   |
+------+---------------------+
|    1 | 2011-06-29 10:48:41 | 
|    2 | 2011-06-29 10:48:44 | 
|    3 | 2011-06-29 10:48:46 | 
|    1 | 2011-06-29 10:48:52 | 
|    2 | 2011-06-29 10:48:53 | 
|    3 | 2011-06-29 10:48:55 | 
+------+---------------------+
mysql> DELETE t1 FROM test t1 INNER JOIN test t2 ON t1.id = t2.id AND t1.d < t2.d;
Query OK, 3 rows affected (0.00 sec)
mysql> SELECT * FROM test;
+------+---------------------+
| id   | d                   |
+------+---------------------+
|    1 | 2011-06-29 10:48:52 | 
|    2 | 2011-06-29 10:48:53 | 
|    3 | 2011-06-29 10:48:55 | 
+------+---------------------+

See also http://dev.mysql.com/doc/refman/5.0/en/delete.html另请参阅http://dev.mysql.com/doc/refman/5.0/en/delete.html

You have to use a "double-barrelled" match on the combination of fields from another query.您必须对来自另一个查询的字段组合使用“双管”匹配。

DELETE FROM tblSpecialTable
WHERE CONCAT(specialid, date) IN (
    SELECT CONCAT(specialid, date)
    FROM (
        SELECT specialid, MAX(date) AS DATE, COUNT(*)
        FROM tblSpecialTable
        GROUP BY 1
        HAVING COUNT(*) > 1) x
    )

use a tmp table, set specialid column is unique.使用 tmp 表,设置 specialid 列是唯一的。 then use below sql: insert into tmp(specailid,date) values(select specialid,date from tplSpecialTable order by date desc)然后在下面使用 sql:插入 tmp(specailid,date) 值(从 tplSpecialTable 中选择 specialid,date 按日期 desc 顺序)

DELETE FROM tblSpecialTable
  WHERE specialid NOT IN 
     (SELECT specialid FROM tblSpecialTable
                 GROUP BY specialid 
                 HAVING COUNT(table.date) > 1 
                 ORDER BY date 
                 LIMIT COUNT(table.date) - 1 )

This isn't a fancy single query, but it does the trick:这不是一个花哨的单一查询,但它可以解决问题:

CREATE TABLE tmp as SELECT * FROM tblspecialtable ORDER BY date DESC;
DELETE FROM tblspecialtable WHERE 1;
INSERT INTO tblspecialtable SELECT * FROM tmp GROUP BY specialid;
DROP TABLE tmp;

The first line creates a temporary table where the values are ordered by date, most recent first.第一行创建一个临时表,其中的值按日期排序,最近的在前。 The second makes room in the original table for the fixed values.第二个在原始表中为固定值腾出空间。 The third consolidates the values, and since the GROUP BY command goes from the top down, it takes the most recent first.第三个合并了值,并且由于 GROUP BY 命令是从上到下的,因此它首先采用最新的。 The final line removes the temporary table.最后一行删除了临时表。 The end result is the original table containing unique values of specialid with only the most recent dates.最终结果是原始表,其中包含只有最近日期的 specialid 唯一值。

Also, if you are programatically accessing your mysql table, it would be best to check if an id exists first, and then use the update command to change the date, or else add a new row if there is no existing specialID.此外,如果您以编程方式访问 mysql 表,最好先检查 id 是否存在,然后使用 update 命令更改日期,或者如果没有现有的 specialID,则添加新行。 Also, you should consider making specialID UNIQUE if you don't want duplicates.此外,如果您不想重复,您应该考虑将 specialID 设为 UNIQUE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM