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))
Related
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.
How can I combine this two update statements :
update Special_quota set Status=0,Additional_msg_quota=0 where User_id not in('1','2','3')
update Special_quota set Status=1,Additional_msg_quota=30 where User_id in('1','2','3')
You can use CASE expression like this:
UPDATE Special_quota
set Status = CASE WHEN User_ID IN('1','2','3') then 1 else 0 end,
additional_msg_quota = CASE WHEN User_ID IN('1','2','3') then 30 else 0 end
UPDATE Special_quota
set Status = CASE WHEN User_ID IN('1','2','3') then 1 else 0 end,
Additional_msg_quota = CASE WHEN User_ID IN('1','2','3') then 30 else 0 end
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.
This is my previous question related to the my query.
MySQL select column which is a value in another column
The problem is that want to do operations on the values extracted and store it back into the original db. I've tried using a update & case but am not able to achieve it.
update msisdn_table
CASE reason
WHEN 'NoAnswer' THEN (case when (NoAnswer>0) then update msisdn_table set NoAnswer = NoAnswer-1 end)
WHEN 'NetworkBusy' THEN (case when NetworkBusy>0 then update msisdn_table set NetworkBusy = NetworkBusy-1 end)
WHEN 'CallRejection' THEN (case when CallRejection>0 then update msisdn_table set CallRejection = CallRejection-1 end)
WHEN 'Unavailable' THEN (case when Unavailable>0 then update msisdn_table set Unavailable = Unavailable-1 end)
END
Any help?
Try it this way if you want to do it one statement
UPDATE msisdn_table
SET NoAnswer = IFNULL(IF(reason = 'NoAnswer',
NULLIF(NoAnswer, 0) - 1, NoAnswer), 0),
NetworkBusy = IFNULL(IF(reason = 'NetworkBusy',
NULLIF(NetworkBusy, 0) - 1, NetworkBusy), 0),
CallRejection = IFNULL(IF(reason = 'CallRejection',
NULLIF(CallRejection, 0) - 1, CallRejection), 0),
Unavailable = IFNULL(IF(reason = 'Unavailable',
NULLIF(Unavailable, 0) - 1, Unavailable), 0)
WHERE reason IN('NoAnswer', 'NetworkBusy', 'CallRejection', 'Unavailable');
Note:
I changed CASE with less verbose IF(), although if you like it better you can use it the same way.
This approach has one possible side effect as it always updates the column(s) either with a new or with old value. It may matter if for example you have a trigger defined on the table.
You want to apply a WHERE clause to make sure that rows with other reason codes are not affected
Here is SQLFiddle demo
update msisdn_table set NoAnswer = NoAnswer-1 where (NoAnswer>0) ;
update msisdn_table set NetworkBusy = NetworkBusy-1 where (NetworkBusy = NetworkBusy-1) ;
update msisdn_table set CallRejection = CallRejection-1 where (CallRejection>0) ;
update msisdn_table set 'Unavailable' = 'Unavailable'-1 where (Unavailable>0) ;
UPDATE tbl SET counts=counts-1 ...
If count is the only column you're updating (or, you don't have other criteria specified in your where clause), then you can just do that in the where clause
UPDATE [Table] SET counts = counts - 1 WHERE counts > 0;
However, if you're updating other columns in the same query, this won't work. But you have options
UPDATE [Table] SET counts = MAX(counts - 1, 0);
or
UPDATE [Table] SET counts = CASE WHEN counts > 0 THEN counts - 1 ELSE 0 END;
UPDATE tbl
SET counts=counts-1
WHERE counts > 0
thanks to #Peter Bailey
this is the example with the WHERE selector .
UPDATE [tbl_multimedia] SET [m_publish] = CASE WHEN [m_publish] = 0 THEN '1' ELSE '0' END WHERE id='1'
Good luck .