or inside case statement not running in mysql - mysql

I m having a query in which inside a case statement i have put a or statement.
The or statement is not running for some reason.
The example -
update shouts set visibility=(case when (visibility = null or visibility =1)
then 2 else 1 end) where shout_id = 788
Now when visibility is null it should update 2 while for no known reason it is updating the field to 1.

Use the IS operator when comparing with NULL. Because comparing something with NULL will lead to unknown when using the normal operators, which is not true.
update shouts
set visibility = case when visibility IS null or visibility = 1
then 2
else 1
end
where shout_id = 788

I want to point out that you can simplify the logic:
update shouts
set visibility = (case when visibility <> 1 then 1 else 2 end)
where shout_id = 788;
Or even:
update shouts
set visibility = 2 - (visibility <> 1)
where shout_id = 788;
Juergen's is the right solution to the particular problem, though.

Related

How to update a record conditionally with a field value in the same record in MySQL? [duplicate]

I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.

Using Four Conditions in a Case When Statement in MYSQL

I was wondering if there's a way to use at least four conditionals in a case when statement in MYSQL.
CASE
WHEN
rank = 0 AND
visibility != 'visibile' AND
rank = 1 AND
visibility = 'visibile'
THEN 'Yes'
WHEN
rank = 1 AND
visibility != 'visibile' AND
rank = 2 AND
visibility = 'visibile'
THEN 'Yes'
WHEN
rank = 2 AND
visibility != 'visibile' AND
rank = 3 AND
visibility = 'visibile'
THEN 'Yes'
ELSE null
END 'Missing in Sequence'
When ever I use more than 2 conditionals I can't seem to get it to work.
Does anyone have any suggestions?
Thanks
Your query will never work logically. Because, visibility = 'visibile' and visibility != 'visibile' are contradicting each other in every case. So, I tried to correct your CASE statement by creating a nested CASE statement.Try it:
CASE WHEN
((SELECT visibility FROM table WHERE rank=0)!='visibile' AND
(SELECT visibility FROM table WHERE rank=1)='visibile')
THEN 'Yes'
WHEN
((SELECT visibility FROM table WHERE rank=1)!='visibile' AND
(SELECT visibility FROM table WHERE rank=2)='visibile')
THEN 'Yes'
WHEN
((SELECT visibility FROM table WHERE rank=2)!='visibile' AND
(SELECT visibility FROM table WHERE rank=3)='visibile')
THEN 'Yes'
ELSE 'Missing in Sequence' END

Stymied building this cross-database query

I'm trying to migrate some data from an old database to a new one with a slightly different schema and my SQL isn't terribly strong.
Old schema: There is a table we'll call "Person" with a field which can have set of permutations of 3 flags. The Person table has a foreign key to another table we'll call "Flags". They Flags table has rows for each of these flag combinations in a String:
1 - Yes No No
2 - Yes Yes No
3 - Yes No Yes
4 - Yes Yes Yes
5 - No Yes No
6 - No Yes Yes
7 - No No Yes
The new schema doesn't require this table (thankfully). These flags are simply fields in the "Person" table now as BIT fields.
What I want to do is something like a:
UPDATE database2.Person SET (flag1, flag2, flag3) VALUES (true, false false) WHERE database1.Person.flag_id = 1;
I could then run 7 different queries changing the IDs and the values accordingly. The problem, of course, is that the above isn't correct SQL. I think I need some kind of JOIN ...or a subselect in the where clause or something?
Stumped on the best way forward. My parting thought here is that this doesn't need to be compressed into a single query, or particularly elegant. I expect to run this query once and be done with it.
You could try something like:
update database2.Person p2 join database1.Person p1 on p1.PersonId = p2.PersonId
set flag1 = case when p1.Flag_id in (1,2,3,4) then true else false end case,
flag2 = case when p1.Flag_id in (2,4,5,6) then true else false end case,
flag3 = case when p1.Flag_id in (3,4,6,7) then true else false end case
(edited for mySQl syntax)
This should work if I'm understanding your question. It assumes you have a Person_Id matching in each table.
UPDATE db2.Person p
JOIN db1.Person p2 ON p.Person_Id = p2.Person_Id
SET p.Flag1 = 1,
p.Flag2 = 0,
p.Flag3 = 0
WHERE p2.Flag_Id = 1;
If the flag values in the Flags table were columns, you could easily run a single query, but as I'm understanding it, it's just a string field. This is an example:
UPDATE db2.Person p
JOIN db1.Person p2 ON p.Person_Id = p2.Person_Id
JOIN db1.Flags f ON p2.Flag_Id = f.Flag_Id
SET p.Flag1 = CASE WHEN f.Flag1 = 'Yes' THEN 1 ELSE 0 END,
p.Flag2 = CASE WHEN f.Flag2 = 'Yes' THEN 1 ELSE 0 END,
p.Flag3 = CASE WHEN f.Flag3 = 'Yes' THEN 1 ELSE 0 END

Updating same flag twice using MYSQL

I have a table named chart with two columns, named UPC_REPORT_ID and UPC_FLAG
I am trying to prepare a query to update a UPC_FLAG=1 if UPC_REPORT_ID=1 and simultaneously I want to update UPC_FLAG=0 where UPC_REPORT_ID !=2
Please give me the suggestions
UPDATE chart SET UPC_FLAG = (CASE WHEN UPC_REPORT_ID=1 THEN 1 ELSE CASE WHEN UPC_REPORT_ID !=2 THEN 0 END END)
UPDATE chart
SET UPC_FLAG = CASE UPC_REPORT_ID
WHEN 1 THEN 1
WHEN 2 then UPC_FLAG
ELSE 0
END
Use WHEN
UPDATE ... set UPC_FLAG=CASE WHEN UPC_REPORT=1 THEN 1 WHEN UPC_REPORT_ID <> 2 THEN 0 END WHERE...
--dmg
The same thing with IF AS Barmar's answer
UPDATE chart
SET UPC_FLAG = IF(UPC_REPORT_ID = 1,1,IF(UPC_REPORT_ID <> 2,2,0))

Update Query based on condition

I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.