MySQL select and then insert or update if exists -


i need find multiple rows related users , insert table or update if record exists current day.

i doing way

select case when (  select   distinct `userid`,   count(distinct `userip`,`userid`) `count`,    @date:=unix_timestamp(curdate())  `tablename`   (`date` >= unix_timestamp(curdate())) group `userid` ) (  update `tablename2` set `count`=`count`,`userid`=`userid`,`date`=`date` `date` >= unix_timestamp(curdate()))  ) else (  insert `tablename2` (`count`,`userid`,`date`) values(`count`,`userid`,`date`); ) end 

but giving me syntax error near update..

how can fix this?

i guessing want 1 row per user , date in tablename2. if so, enforce rule unique index:

create unique index idx_tablename2(userid, date) 

then database enforces it.

your sql mess, think can see trying do. basic idea insert . . . on duplicate key update. think following want:

insert `tablename2` (`count`, `userid`, `date`)      select `userid`,  count(distinct `userip`, `userid`) `count`,              unix_timestamp(curdate())     `tablename`      `date` >= unix_timestamp(curdate())     group `userid`     on duplicate key update `count` = values(`count`); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -