I have a table with columns id
, user_id
, post_id
, like
, views
. When a user click the Like button, a row will be added to the table with the corresponding user_id
and post_id
, and like
will be set to 1
. If the row already exist and the like
column already is 1
, nothing will happen.
When the user visits a particular page, a row will be added to the table with the corresponding user_id
and post_id
, and view
will be set to 1
. However if the row already exist, the value in view
will be incremented.
Problem: I tried using INSERT INTO mytable ... ON DUPLICATE KEY UPDATE like = 1
, but the primary key of the table is id
and not user_id
or post_id
. If I set the primary key to either user_id
or post_id
, there will be a problem because the row will only be duplicated if there exist a row with the same user_id
and post_id
.
In this situation, how should the query be built?
Or will a better situation be to split the table into 2 tables, one for likes
and 1 for views
. If this is the case, I still need to make the row unique based on both user_id
and post_id
columns.
Or do multiple SQL queries?
Please advise, thanks!