Foxel :
I would like to update a row only if a condition on another row is met.
something like:
IF (SELECT `allow` FROM `accountlist` WHERE `id`='myid')<'1234'
THEN
IF (UPDATE `accountlist` SET `upvotes`=`upvotes`+1 WHERE `user`='myuser')
THEN (UPDATE `accountlist` SET `allow`='1234' WHERE `id`='myid');
END IF;
ELSE (/*return false*/);
END IF
in other words, if allow is lower than 1234 on myid, increment upvotes on another row and if you succeded set allow to 1234, otherwise return false or some error values.
Is it possible to run multiple queries when a condition is met?
forpas :
You need CASE expressions like this:
UPDATE `accountlist`
SET `upvotes` = CASE WHEN `user`='myuser' THEN `upvotes` + 1 ELSE `upvotes` END,
`allow` = CASE WHEN `id` = 'myid' THEN '1234' ELSE `allow` END
WHERE (SELECT `allow` FROM `accountlist` WHERE `id`='myid') < '1234'
AND EXISTS (SELECT 1 FROM `accountlist` WHERE `user`='myuser')
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=410627&siteId=1