sql - duplicate rows MySQL -
hey using query this:
insert likes( likes_memory_id, likes_comment_id, likes_owner_id, likes_like ) values ( :likes_memory_id, :likes_comment_id, :likes_owner_id, :likes_like)
when ever user click button, query adds new row. query allows multiple time. prevent may use select statement , might succeed in 2 queries assue there better way it. (i made research if not exists statement didnt understand ) how avoid multiple likes?
the simplest create unique index on columns want unique;
create unique index uq_mem_own on likes( likes_memory_id, likes_owner_id );
...and insert likes using insert ignore, insert value if it's not prevented index, otherwise ignore it;
insert ignore likes( likes_memory_id, likes_owner_id, likes_like ) values ( :likes_memory_id, :likes_owner_id, :likes_like)
Comments
Post a Comment