Query using CASE WHEN
I am trying to write a query like this and I am getting an error. This is my first use case, so I believe there will be a problem.
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
source to share
You can rewrite it 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 condition or
in ()
to match 9211
against parent_post_id
and attachment_id
using or operation, also confusing conditions that you have in your query arise like parent_post_id
it attachment_id
can be equal to 2 values ββat the same time, maybe you are looking for
WHERE (attachment_id = 8990 AND parent_post_id = 9221 )
OR (attachment_id = 9211 AND parent_post_id = 383)
source to share
update
- update table set col=value where condition
. Usage case
doesn't change this. case
can only be used to express 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
source to share