duplicate rows MySQL

suyilmaz

Hey i am using a query like this:

INSERT INTO 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 an user click the like button, this query adds a new row. so this query allows to like multiple time. to prevent this i may use a select statement and i might succeed in two queries but i assue there is a better way to do it. (I made research about if not exists statement but i didnt understand too much ) How do I avoid multiple likes?

Gorkem Yontem

The simplest is to create a unique index on your columns which you want unique;

CREATE UNIQUE INDEX uq_mem_own ON likes(
 likes_memory_id, likes_owner_id
);

...and insert likes using INSERT IGNORE, which will insert the value if it's not prevented by the index, otherwise just ignore it;

INSERT IGNORE INTO likes( 
 likes_memory_id, 
 likes_owner_id, 
 likes_like
 ) VALUES (
 :likes_memory_id, 
 :likes_owner_id, 
 :likes_like)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Delete all Duplicate Rows except for One in MySQL?

From Dev

MySQL delete duplicate rows in table

From Dev

Remove duplicate rows on many to many table (Mysql)

From Dev

Find and Delete Duplicate rows in MySQL

From Dev

MySQL find duplicate rows between certain days

From Dev

MySQL removing Duplicate Rows with Primary Key

From Dev

Mysql delete duplicate rows + sort condition

From Dev

mysql: count duplicate rows from all the groups

From Dev

Merge references to duplicate rows in mysql

From Dev

MySQL count rows with two duplicate column values

From Dev

MySQL query returns duplicate rows

From Dev

Merge references to duplicate rows in mysql

From Dev

Duplicate rows in mysql

From Dev

Delete duplicate rows in mysql

From Dev

MySQL ManyToMany show duplicate rows

From Dev

MySQL - Add duplicate rows to archive table, then delete duplicate rows

From Dev

MySQL fulltext search returns duplicate rows

From Dev

many to many query mysql return duplicate rows

From Dev

duplicate rows MySQL

From Dev

MySQL IN() with duplicate rows

From Dev

counting duplicate rows in mysql

From Dev

Remove duplicate rows on many to many table (Mysql)

From Dev

How to delete duplicate rows from mysql

From Dev

Find and Delete Duplicate rows in MySQL

From Dev

Duplicate Rows Loading Data From MySQL to DataGridView

From Dev

mysql delete duplicate rows from table

From Dev

Get the duplicate rows with the number of occurrence in MYSQL

From Dev

Intelligently combine duplicate rows in MySQL select results

From Dev

Finding duplicate rows in a MySQL table