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`
hasil dalam 2 baris ditampilkan
+----+----------+------------+
| id | username | date |
+----+----------+------------+
| 1 | john | 2015-01-01 |
| 3 | john | 2015-01-01 |
+----+----------+------------+
2 rows in set (0.03 sec)
Sunting:
sesuai permintaan OP, miliki kolom untuk menandai penipuan untuk pekerjaan selanjutnya, sebagai lawan dari pernyataan pilih. Perhatikan bahwa Anda dapat Alter Table
dan tambahkan kolom bendera yang dapat dibatalkan ini, atur, gunakan nilai sesuka Anda, nanti Alter Table
dan jatuhkan.
Tapi saya akan mulai dari sini dengan membuat tabel dengan kolom bendera baru:
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)