Ich habe dafür eine neue Flag-Spalte verwendet und hatte den Vorteil, bei Ihrer anderen Frage hier zu helfen .
Demo-Schema-Einrichtung
create table table1
(
id int auto_increment primary key,
username varchar(30) not null,
`date` date not null,
dupeFlag int null, -- <---- New flag column, nullable, ignored on inserts below
firstFlag int null -- <-- was first dupe for day? 2=yes, ignored on inserts below
);
insert table1 (username,`date`) values ('john','2015-01-01');
insert table1 (username,`date`) values ('kim','2015-01-01');
insert table1 (username,`date`) values ('john','2015-01-01');
insert table1 (username,`date`) values ('john','2015-02-01');
insert table1 (username,`date`) values ('john','2015-03-01');
insert table1 (username,`date`) values ('john','2015-03-01');
insert table1 (username,`date`) values ('kim','2015-01-01');
insert table1 (username,`date`) values ('kim','2015-02-01');
Update-Anweisung , legen Sie Duplikate fest und zuerst für Tag basierend auf PK-ID
update table1 t1
join
( select username,`date`,count(*) as theCount,min(id) as minForGroup
from table1
group by username,`date`
having theCount>1
) inr
on inr.username=t1.username and inr.`date`=t1.`date`
set dupeFlag=1,
firstFlag=if(id=inr.minForGroup,2,666);
select * from table1;
+----+----------+------------+----------+-----------+
| id | username | date | dupeFlag | firstFlag |
+----+----------+------------+----------+-----------+
| 1 | john | 2015-01-01 | 1 | 2 |
| 2 | kim | 2015-01-01 | 1 | 2 |
| 3 | john | 2015-01-01 | 1 | 666 |
| 4 | john | 2015-02-01 | NULL | NULL |
| 5 | john | 2015-03-01 | 1 | 2 |
| 6 | john | 2015-03-01 | 1 | 666 |
| 7 | kim | 2015-01-01 | 1 | 666 |
| 8 | kim | 2015-02-01 | NULL | NULL |
+----+----------+------------+----------+-----------+
8 rows in set (0.00 sec)