Reputation: 405
I have a table with 2 columns, they are 'user' and 'action' and are used to denote when a user has completed a certain action. An entry should only be made if a user has not made the given action before so I feel something like the following is what is required:
IF NOT EXISTS (select * from actiontable WHERE user=1 and action=5) THEN
INSERT INTO actiontable VALUES(1,5);
Yet this seems to be incorrect syntax and I can't find how I should go about this. Is this possible? Or is there some way to use a primary key or unique key to achieve this?
Upvotes: 0
Views: 103
Reputation: 6120
You could create a primary key on user,action and then try
INSERT INTO actiontable (`user`,`action`) VALUES (1,5);
It will fail if the entry already exists. If you do not want to raise an error, you could try
INSERT IGNORE INTO actiontable (`user`,`action`) VALUES (1,5);
Upvotes: 2