Ik ging hiervoor met een nieuwe vlaggenkolom en had het voordeel dat ik hier hielp met je andere vraag .
Demoschema instellen
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-verklaring , stel dupes in en eerste voor dag op basis van 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)