I have a table in MySQL with multiple of reported events (primary key, date submitted, and text). Users can “like”, or “+1” the reports, similar to facebook/google. Right now, a user can “like” a single event infinite times. What’s the best way of arranging the database to check if he’s agreed to this even before?
I considered adding a column that can store a huge amount of text and adding a user to this text block if he agrees (comma seperated). I could then explode the text later and search it to see if his name is in there. However, I figured there must be a better way of going about this – any suggestions?
I’d use three tables:
event,useranduser_likes_eventInsert some values:
Every user now likes every event once. If Tom attempts to like Event 1 again, a primary key constraint error is thrown:
If you don’t want errors thrown, you could try a conditional insert:
See this question for ideas on
INSERT WHERE NOT EXISTSalternatives.See the SELECT Syntax for more information on the
DUALtable.