mysql - SQL Query Update field depending on subquery -
i need sql query. table users has 3 fields: name, iduser, idgroup.
one user can appear many times on same table same iduser , different idgroups.
field name should update new value if has changed.
if there no exists row given iduser , idgroup has inserted new row.
if exists(select 1 users iduser=3 , idgroup=4) begin update users set name='new name' iduser=3 , idgroup=4 end else insert users values('name',5,3) end
my sql query sintax wrong can't use it, how achieve need?
i'm using mysql database (managed phpmyadmin) , doing insert lua script.
you need make sure have right unique keys in place.
alter table users add unique `unique_index`(`iduser`, `idgroup`);
then, should able run insert/update:
insert users (name, iduser, idgroup) values ('name value',5,3) on duplicate key update name = values(name) name <> values(name)
Comments
Post a Comment