Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据日期和状态在MYSQL中复制_Mysql_Duplicates_Updates - Fatal编程技术网

根据日期和状态在MYSQL中复制

根据日期和状态在MYSQL中复制,mysql,duplicates,updates,Mysql,Duplicates,Updates,这是我当前查找重复电话号码的查询 SELECT id, cust_num, entry_date FROM nums GROUP BY cust_num HAVING count(*) >= 2 ORDER BY id DESC 然而,现在我希望根据标准一次更新它们 我只想更新比原始版本id高的更新版本。 而且只有当他们有一定的地位 下面是一个例子,我将根据从数据库中提取的副本列表更新内容 ID | Num | date

这是我当前查找重复电话号码的查询

SELECT 
    id, cust_num, entry_date 
FROM 
    nums 
GROUP BY 
    cust_num 
HAVING 
    count(*) >= 2 
ORDER BY 
    id 
DESC
然而,现在我希望根据标准一次更新它们

我只想更新比原始版本id高的更新版本。 而且只有当他们有一定的地位

下面是一个例子,我将根据从数据库中提取的副本列表更新内容

 ID |  Num | date    | status
  1    555    Sep-12   NEW    (this row wouldnt update first instance of 555)
  33   555    Oct-12   NEW    (this row would update, duplicate with NEW as status)
  42   333    Dec-12   NEW    (this row wouldn't update first instance of 333)
  5    555    Jan-13   ACTIVE (this row wouldnt update, duplicate but wrong status)
  66   333    Feb-14   NEW    (this row would update, duplicate with NEW as status)
  6    555    Jan-13   NEW    (this row would update, duplicate with NEW as status)
  77   333    Mar 15   ACTIVE (this row wouldnt update, duplicate but wrong status)
所以真正的问题是,我将使用什么查询来提取所有这样的重复项,然后根据它们的状态进行更新

UPDATE nums n SET ... WHERE n.status='NEW' AND (select count(*) from nums where num = n.num and id < n.id and status = 'NEW') > 0;

为要更新的内容添加SET语句。

以下是选择。链接到

将状态更新为已更新


我看不出这有什么帮助。也许我很愚蠢。小组成员在哪里。怎么才能找到复制品???别担心,我帮你找到了。我要去摆弄小提琴。。。袖手旁观
select * from mytable as m where `status` = 'NEW' and exists 
    (select * from mytable as mm where mm.`id` < m.`id` and mm.`num` = m.`num`)
update mytable as m set m.`status` = 'UPDATED' where `status` = 'NEW' and exists 
(select * from mytable as mm where mm.`id` < m.`id` and mm.`num` = m.`num`)