onin
onin

Reputation: 5760

MySQL IF expression returns error

SELECT last_played INTO @lp FROM sr WHERE creator_id = 1 AND playing = 1 LIMIT 1;
SELECT stream_id INTO @sid FROM account_stream WHERE owner = 1 AND enabled = 1 LIMIT 1;
UPDATE sr SET last_played = 1412259166 WHERE creator_id = 1 AND playing = 1 LIMIT 1;
IF @lp != NULL THEN UPDATE streams SET duration = (duration + (1412259166 - @lp)) WHERE id = @sid LIMIT 1; END IF;

What I am trying to do: when @lp returns something (in other words, when playing is 1 in the first query), then execute the last update streams query.

I get this error in phpmyadmin tho:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @lp != 0 THEN UPDATE streams SET duration = (duration + (1412259166 - @lp)) W' at line 1

I've never worked with mysql if conditions before, so, can anyone please help me?

Upvotes: 0

Views: 68

Answers (1)

Benvorth
Benvorth

Reputation: 7722

 UPDATE streams SET duration = (duration + (1412259166 - @lp)) 
 WHERE @lp IS NOT NULL AND id = @sid 

Upvotes: 1

Related Questions