0

I'm running a database under the Marian engine and I have two tables:

Users(id, score)
Replies(user ID, reply)

Now I have to write a stored procedure that, given a number X, increments the field score by 1 for all the users that have replied with X.

How can I do that? I tried to do an update query, but MariaDB doesn't seem to work with the from clause.

Thanks in advance

1
  • edit the question with some sample data and desired result would helpful. Commented Apr 18, 2018 at 5:52

1 Answer 1

1

The update query itself is straightforward:

UPDATE Users u
SET score = score + 1
WHERE EXISTS (SELECT 1 FROM Replies r WHERE r.userID = u.id AND reply = 'X');

Inside a stored procedure, it might look like:

DELIMITER //
CREATE PROCEDURE addScore (reply INT)
BEGIN
SET @query = 'UPDATE Users u
    SET score = score + 1
    WHERE EXISTS (SELECT 1 FROM Replies r WHERE r.userID = u.id AND reply = ?)'

SET @x = reply;
PREPARE stmt FROM @query;
EXECUTE stmt USING @x;
DEALLOCATE PREPARE stmt;
END; //
DELIMITER ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.