This MySQL will increment a user’s reputation by 5 when an upvote is submitted.
UPDATE user_profiles
SET reputation = reputation +5
WHERE user_id = $question_author_id;
Now I want to make a distinction:
- if the upvote is for a question, the increment should be 5,
- if the upvote is for an answer, the increment should be 10.
An answer can be identified by the presence of an integer in the field forum_qa_parent_id of table forum_qa.
So I came up with this but it doesn’t work:
UPDATE user_profiles
IF (SELECT forum_qa_parent_id
FROM forum_qa
WHERE forum_qa_id = $question_id) IS NOT NULL
THEN SET reputation = reputation +10
WHERE user_id = $question_author_id;
ELSE SET reputation = reputation +5
WHERE user_id = $question_author_id;
END IF;
Anyone care to show me how to make this work?
You need to use a CASE statement if you’re doing logical comparisons within a query. http://dev.mysql.com/doc/refman/5.0/en/case-statement.html
Something along those lines – you might need to touch that one up a bit.