-1

im using

CREATE FUNCTION UPDATEGames(usernameIN CHAR(15) ,opponientIN CHAR(15) ,mFilePath TINYTEXT,oFilePath TINYTEXT)
BEGIN
UPDATE games  
IF (username = opponientIN AND FilePath = oFilePath) THEN SET opLastTurn = NOW(),Turn=Turn+1 
ELSEIF (username = usernameIN AND FilePath = mFilePath) THEN SET myLastTurn = NOW(),Turn=Turn+1 
END IF 
END;

but it didnt worked, what am i ding wrong?and if it isnt possible to do it so then how can i simulate this logic?

12
  • 1
    The body of a stored function must be a sequence of valid SQL statements. What you have here … isn't that. How familiar are you with SQL? It's not clear what you're trying to do here, nor that a stored function is an appropriate way to accomplish it. Commented Aug 19, 2017 at 7:01
  • what im trying to do is about the logic of my game (not important if you dont understand the logic ) and the SQL statements are Correct (i have tested each part of it without if else and function ) Commented Aug 19, 2017 at 7:11
  • Well, by introducing the if your sql statement is no longer valid. Within the update you can only use the if function, not the if statement. And we do need to understand the logic because otherwise we cannot suggest a correct solution. Commented Aug 19, 2017 at 7:25
  • 1
    You ask "how can i simulate this logic" and then state that it's "not important if you dont understand the logic"? It's your job to tell us what are trying to do, that's what duskwuff meant. But if I had to guess, you might want to try UPDATE games set opLastTurn = NOW(),Turn=Turn+1 where username = opponientIN AND FilePath = oFilePath (and a 2nd update for usernameIN), without the if-thingy. Have an index on username, filepath. Also, a function needs to return something, so maybe use a procedure. Commented Aug 19, 2017 at 7:28
  • 1
    His question was not about your logic. His question was about what you want to do. Valid answers to that could be: "I want to update rows in my table for both players that are given as a parameter", or "I already know how to update rows in my table using 2 updates, but I really want to do it in one query, and also I am trying to learn about the "if()"-syntax" or "I have tested each part of it, the code is the following: <insert the code you successfully tested>. Now I want to do exactly this in a procedure." Commented Aug 19, 2017 at 7:39

1 Answer 1

1

Within the update you can only use the if function, not the if statement. The if statement can only be used to execute different sql statements. Furthermore, the where criteria must be used in the update to restrict the records to be updated.

So, there is no need to use if statement at all, just use two updates. There is no return value either, so I also changed function to procedure.

DELIMITER //
CREATE PROCEDURE UPDATEGames(usernameIN CHAR(15) ,opponientIN CHAR(15) ,mFilePath TINYTEXT,oFilePath TINYTEXT)
BEGIN
    UPDATE games SET opLastTurn = NOW(),Turn=Turn+1 WHERE username = opponientIN AND FilePath = oFilePath;
    UPDATE games SET myLastTurn = NOW(),Turn=Turn+1 WHERE username = usernameIN AND FilePath = mFilePath;
END//
DELIMITER ;
Sign up to request clarification or add additional context in comments.

2 Comments

yes but two wheres means two time search iteration and i was up to make it one ,and about the if statment you said is it possible to use 'case when' instead?
Case statement will also execute one or the other updates, not both. Again, there is a case xpression than you can use instead. As I explained in my previous comment, this particular issue is not a good example for practicing conditional statements. If username and filepath fields are indexed, then these queries will be really fast.

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.