create table table1
(
id int auto_increment primary key,
username varchar(30) not null,
`date` date not null
);
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');
SELECT t1.*
from table1 t1
join
(
select username,`date`,count(*)
from table1
group by username,`date`
having count(username)>1
) inr
on inr.username=t1.username and inr.`date`=t1.`date`
resultaten in 2 rijen weergegeven
+----+----------+------------+
| id | username | date |
+----+----------+------------+
| 1 | john | 2015-01-01 |
| 3 | john | 2015-01-01 |
+----+----------+------------+
2 rows in set (0.03 sec)
Bewerken:
volgens OP-verzoek, een kolom hebben om dupes te markeren voor later werk, in tegenstelling tot een select statement. Let op:u kunt Alter Table
en voeg deze nullable vlagkolom toe, stel deze in, gebruik waarden op uw gemak, later Alter Table
en laat het vallen.
Maar ik zal hier gewoon beginnen met de tabel maken met een nieuwe vlagkolom:
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
);
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');
update table1 t1
join
( select username,`date`,count(*)
from table1
group by username,`date`
having count(username)>1
) inr
on inr.username=t1.username and inr.`date`=t1.`date`
set dupeflag=1;
-- 2 rows affected
select * from table1;
+----+----------+------------+----------+
| id | username | date | dupeflag |
+----+----------+------------+----------+
| 1 | john | 2015-01-01 | 1 |
| 2 | kim | 2015-01-01 | NULL |
| 3 | john | 2015-01-01 | 1 |
| 4 | john | 2015-02-01 | NULL |
| 5 | john | 2015-03-01 | NULL |
+----+----------+------------+----------+
5 rows in set (0.00 sec)