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`
risulta in 2 righe visualizzate
+----+----------+------------+
| id | username | date |
+----+----------+------------+
| 1 | john | 2015-01-01 |
| 3 | john | 2015-01-01 |
+----+----------+------------+
2 rows in set (0.03 sec)
Modifica:
come da richiesta OP, avere una colonna per contrassegnare i duplicati per il lavoro successivo, al contrario di un'istruzione select. Nota che puoi Alter Table
e aggiungi questa colonna flag nullable, impostala, usa i valori a tuo piacimento, in seguito Alter Table
e rilascialo.
Ma ricomincerò da qui con la tabella di creazione con la nuova colonna flag:
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)