sql - MySQL - on duplicate key - CASE WHEN THEN ELSE doesnt work -


i want insert entry (uid, a, b) database. if entry existed already, update 'a' , 'b' column condition: (1 = old entry; [2] = new entry)

a = (b[1] == b[2]) ? a[1] : a[2]; b = b[2] + 1; 

my query:

insert tablename (uid,a,b)  values (uid,a[2],b[2])  on duplicate key  update  a= (select case b when b[2] else a[2] end),          b= b[2]+1; 

ex1:

old entry : uid = 1; = 2; b = 4; new entry : uid = 1; = 3; b = 4; -> should be: = 2; b = 5; result: = 3; b = 5; -> fail 

ex2:

old entry : uid = 1; = 2; b = 4; new entry : uid = 1; = 3; b = 6; -> = 3; b = 7; (my code works in ex) -> ok 

thank much.

answer 1: remove select key make work

insert tablename (uid,a,b)  values (uid,a[2],b[2])  on duplicate key  update  a= (case b when b[2] else a[2] end),          b= b[2]+1; 

answer of p-jairaj

insert tablename (uid,a,b)  values (uid,a[2],b[2])  on duplicate key  update  a= (if(b=b[2],a,a[2])),          b= b[2]+1; 

try this:

insert tablename (uid,a,b)  values (uid,a[2],b[2])  on duplicate key  update  a= (if(b=b[2],a,a[2])),          b= b[2]+1; 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -