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)
updates 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
Related
I have to check the value of #inv_status with below MySQL code but not getting the desired output.
I am expecting "success 1" but it is giving 0, please suggest.
SET #ip_AllocationId = 1;
SET #ip_status = NULL;
SET #inv_status = 4;
SELECT CASE WHEN #inv_status IN(
SELECT
CASE
WHEN #ip_AllocationId = 1 AND ISNULL(#ip_status) THEN (SELECT CONCAT("1",",","4"))
WHEN #ip_AllocationId = 1 AND #ip_status=1 THEN 1
WHEN #ip_AllocationId = 1 AND #ip_status=2 THEN 4
ELSE NULL
END AS filter
) THEN "1" ELSE "0" END AS "success";
Your code is equivalent to this:
SET #ip_AllocationId = 1;
SET #ip_status = NULL;
SET #inv_status = 4;
SELECT #inv_status = (
CASE
WHEN #ip_AllocationId = 1 AND #ip_status IS NULL THEN CONCAT('1',',','4')
WHEN #ip_AllocationId = 1 AND #ip_status=1 THEN 1
WHEN #ip_AllocationId = 1 AND #ip_status=2 THEN 4
END
) AS success;
What you are doing is compare #inv_status to the string '1,4' because the boolean expression #ip_AllocationId = 1 AND #ip_status IS NULL of the 1st branch of the CASE expression is TRUE.
So the result is FALSE and you get 0.
I suspect that you want is something like this:
SET #ip_AllocationId = 1;
SET #ip_status = NULL;
SET #inv_status = 4;
SELECT FIND_IN_SET(#inv_status,
CASE
WHEN #ip_AllocationId = 1 AND #ip_status IS NULL THEN CONCAT('1',',','4')
WHEN #ip_AllocationId = 1 AND #ip_status=1 THEN '1'
WHEN #ip_AllocationId = 1 AND #ip_status=2 THEN '4'
END
) > 0 AS success;
This way you check the existence of #inv_status in a comma separated list of values instead of a straight comparison.
See the demo.
The IN-comparison operator expects a parameter to be a set, not a string. In your values it returns a set but that set constant single string '1,4' which is not same as 1.
How do you ignore records not inside the case statement when using CASE/WHEN/THEN?
For example, this statement will update three matching records as expected but will make all student records that do not match a WHEN/THEN clause to NULL
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
END)
WHERE year = '2019';
How can you skip over records not inside the CASE statement and only change records that have a matching clause?
For skipping records not in case statement, you can use something like this
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
END)
WHERE year = '2019' AND student IN ('10','17','26');
There a two solutions I think :
Add a where clause
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
END)
WHERE year = '2019' AND student IN ('10','17','26');
Use else statement but that will scan the whole table for nothing :
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
ELSE student
END)
WHERE year = '2019';
You could use a default case for that:
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
ELSE student
END)
WHERE year = '2019';
Otherwise, if you want to minimize the load, just add all known student values to the WHERE clause. This triggers an update only on those rows that are really affected by a change
You can use in or = operator
WHERE year = '2019' AND (student = '10' or student = '17' or student ='26');
I have a requirement to get the result from the first matched condition and if it finds result in that level then return from there or go to Next level to
find the result .
Any help appreciated.
This is a prioritization query (with ties). One method uses dense_rank():
select rsc.*
from (select rsc.*,
dense_rank() over (order by case when rsc.entityId = :accountId and rsc.entityTypeCode = 'A' then 1
when rsc.entityId = :companyId and rsc.entityTypeCode = 'C' then 2
when rsc.entityId = :issuerId and rsc.entityTypeCode = 'I' then 3
else 4
end) as seqnum
from CurrentRuleSetContents rsc
where (rsc.entityId = :accountId and rsc.entityTypeCode = 'A') or
(rsc.entityId = :companyId and rsc.entityTypeCode = 'C') or
(rsc.entityId = :issuerId and rsc.entityTypeCode = 'I') or
(rsc.industryCode = :industryCode)
) rsc
where seqnum = 1;
I've browsed the forums. I've tested several different methods. I just can't get this to work.
GOAL:
Update the next column if an answer exists in the previous column
Q1 IS NULL at this point and I want it set to 1
Q2 IS NULL and I want it to stay NULL if Q1 IS NULL, If Q1 has an answer, update this column to 1
PROBLEM:
Both columns always update to 1
ATTEMPTS:
UPDATE Story1_Responses
SET Q1 = IFNULL(Q1,'1'),
Q2 = CASE WHEN Q1 IS NULL THEN NULL ELSE '1' END
WHERE UserID = 16
UPDATE Story1_Responses
SET Q1 = IFNULL(Q1,'1'),
Q2 = CASE WHEN IFNULL(Q1,'') = '' THEN NULL ELSE '1' END
WHERE UserID = 16
UPDATE Story1_Responses
SET Q1 = IFNULL(Q1,'1'),
Q2 = CASE WHEN COALESCE(Q1, '') = '' THEN NULL ELSE '1' END
WHERE UserID = 16
UPDATE Story1_Responses
SET Q1 = IFNULL(Q1,'1'),
Q2 = CASE WHEN Q1 IS NOT NULL THEN '1' ELSE IFNULL(Q2,NULL) END
WHERE UserID = 16
THOUGHTS:
It seems to me that MySQL updates the first column prior to the second column being evaluated even though they are in the same update statement execution.
Order is important. First set Q2 (because it checks the previous value of Q1), then set Q1.
UPDATE Story1_Responses
SET Q2 = CASE WHEN Q1 IS NULL THEN NULL ELSE '1' END,
Q1 = IFNULL(Q1,'1')
WHERE UserID = 16;
SQLFiddle
It seems I have big problems with conditional queries.
I have to do a conditional update. I write here what I would like to do:
IF(SELECT tipo FROM abbonamento WHERE idU = 17) = 'punti' THEN
UDPATE abbonamento SET punti = punti - 1
ELSE
UPDATE abbonamento SET bonus = bonus - 1
Obviously this doesn't work.
Any idea?
MySQL supports IF statement.
UPDATE abbonamento
SET punti = IF(tipo = 'punti', punti - 1, punti),
bonus = IF(tipo <> 'punti', bonus - 1, bonus)
WHERE id = 17
or you can also use CASE
UPDATE abbonamento
SET punti = CASE WHEN tipo = 'punti' THEN punti - 1 ELSE punti END,
bonus = CASE WHEN tipo <> 'punti' THEN bonus - 1 ELSE bonus END
WHERE id = 17