I am trying to write a query like this and I am getting an error. It is my first time using case, so that's where I believe the problem to be.
UPDATE my_table
CASE
WHEN downloads IS NULL THEN
SET downloads = 1
ELSE
SET downloads + 1
END
WHERE attachment_id = 8990
AND parent_post_id = 9221
OR attachment_id = 9211
AND parent_post_id = 383
You can rewrite it as below
UPDATE my_table
SET downloads = CASE WHEN downloads IS NULL THEN 1
ELSE downloads + 1 END
WHERE attachment_id = 8990
AND (parent_post_id = 9221
OR attachment_id = 9211 )
AND parent_post_id = 383
Also you need to group your or
condition in ()
in order to match 9211
against parent_post_id
and attachment_id
with or operation,also there is confusing conditions you have in your query how parent_post_id
and attachment_id
can be equal to 2 values at same time may be you are looking for
WHERE (attachment_id = 8990 AND parent_post_id = 9221 )
OR (attachment_id = 9211 AND parent_post_id = 383)
update
s syntax is update table set col=value where condition
. Using case
doesn't change that. case
can only be used to return an expression, so:
UPDATE my_table
SET downloads = CASE WHEN downloads IS NULL THEN 1 ELSE downloads + 1 END
WHERE attachment_id = 8990 AND
parent_post_id = 9221 OR
attachment_id = 9211 AND
parent_post_id = 383
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.