Kiwizoom
Kiwizoom

Reputation: 443

Mysql update column on first occurrence of each username

I'm trying to mark the first occurrence per day of each username. I already have all concerning people marked as dupe = 1. The first for that day should get set to dupe = 2.

Like in layman's terms, If first entry of this username for this day, mark column = 2. Do for each day.

Based on this question https://stackoverflow.com/a/12065956/1811162 I can make this

Select *
from (
     select * from table WHERE dupe=1 order by date desc
     ) x
group by date

Which returns one member of each duplicate I'm looking for, but I'd like to set that one = 2. I am having trouble making this an update statement. Or would this even work as an update statement? I only want the first member to set.

The result I want is -

Select username, dupe where dupe!= 0;

Day 1
Bob   - 2
Kathy - 2
Bob   - 1
Kathy - 1
Kathy - 1

Day 2
Kathy - 2
Kathy - 1
Bob - 2
Kathy - 1

What I tried is

UPDATE table set dupeflag=2 from (
select * from
from (
    select * from table WHERE dupeflag=1 order by date desc
    ) x
group by date
)

but no luck. probably very wrong

Upvotes: 2

Views: 853

Answers (1)

Drew
Drew

Reputation: 24960

I went with a new flag column for this, plus had the benefit of helping with your other question here.

Demo schema setup

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 statement, set dupes and first for day based on 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)

Upvotes: 2

Related Questions