IF THEN + 1 To Specific row/column? - mysql

So I've been working on a new project. I understand how to update a certain rows column value:
UPDATE PlayerInfo SET GearLevel = GearLevel +1 WHERE UID = "76561198008596823" ;
But I want to add some checks in it.
In Words.
IF UID = "76561198008596823" AND BankMoney = 25000000 AND GearLevel =
15 THEN GearLevel + 1 AND BankMoney - 15000000
So I tested with:
UPDATE PlayerInfo SET GearLevel =
CASE WHEN GearLevel = 15 THEN GearLevel +1
Else
0
END ;
But this just added 1 to all values in the column that was at 15.
How can I add the checks to the CASE and also subtract BankMoney? Or is there an easier way to achieve this?

You may not need to use a CASE expression here:
UPDATE PlayerInfo
SET
GearLevel = GearLevel + 1,
BankMoney = BankMoney - 15000000
WHERE
UID = '76561198008596823' AND
BankMoney = 25000000 AND
GearLevel = 15;
A CASE expression would be needed if you wanted to update every record with some value. Then, its logic would let you carefully decide which records get updated. But if you only want to target accounts with the three conditions you gave us (UID=7656..., BankMoney=2500000 and GearLevel=15), then you may just add this directly to the WHERE clause.

Related

MYSQL - UPDATE multiple fields based on single CASE statement

OBJECTIVE
I am looking for a way to update one or both fields using the same CASE statement
UPDATE vendor
SET special_cost =
(
CASE
WHEN
cost > 14
THEN
14
ELSE
SET special_cost = cost, cost = 14
END
)
WHERE upc = '12345678912345'
LOGIC
This is some logic that explains what I want to happen above.
if ($cost > 14) {
$special_cost = 14;
} else {
$special_cost = $cost;
$cost = 14;
}
It does not seem that there is a way to do this with a single CASE statement. Perhaps, this can be done with the mysql IF statement?
You are referring to case expressions not case statements.
You appear to want something like this:
UPDATE vendor
SET special_cost = GREATEST(cost, 14),
cost = 14
WHERE upc = '12345678912345';

Update database record by selected id and reset the initial record

Please how do i update all record to 0 and set only the selected ID to 1
UPDATE address SET default_addr = 1
WHERE addr_id = 100 AND user = 'peter'
The above query will update the selected address to 1 which is good, but i want to set other address or the old selected default to 0 with one query
In MySQL, you can do:
UPDATE address
SET default_addr = (addr_id = 100 AND user = 'peter');
(This shorthand uses the fact that MySQL treats booleans as numbers in a numeric context, with "0" for false and "1" for true.)
If you want only one default address for the user named Peter, then use a where:
UPDATE address
SET default_addr = (addr_id = 100)
WHERE user = 'peter';
I suspect this is the logic that you really want.
use a conditional update using case statement
update address set default_address = case when addr_id = 100 and user = 'peter' then 1 else 0 end
here is a functional example
I built a sample schema. These are often helpful to provide in your future questions.

Update Same mysql field twice in single query

I am not sure if its possible or not, Just want to know if it is. I have column plan_popular which has default value 0. Lets same i have a list :
Plan Name | plan_popular | amount
===================================
plan A 0 25.00
plan B 1 50.00
plan C 0 90.00
This is how i am doing:
$stmt = "update {CI}plans set plan_popular = 0";
$this->db->query($stmt);
$stmt2 = "update {CI}plans set plan_popular = 1 where plan_id = ?";
$this->db->query( $stmt2, array($plan_id) );
Now i have set the plan C to make. Now i want to reset it and want to make popular plan C to 1. What i am doing is running two queries, One i reset and make the plan_popular 0 and the second is get the update the plan C to 1 with there id. Is it possible in single query?
You can use an expression to determine the value to assign:
UPDATE {CI}plans
SET plan_popular = IF(plan_id = ?, 1, 0);
try this,
UPDATE {CI}plans
SET `plan_popular` = CASE `Plan Name`
WHEN 'plan C' THEN 1
ELSE 0
END
WHERE `Plan Name` IN((select `Plan Name` from {CI}plans where plan_popular=1 ) , 'plan C');
Updates can be expensive, what with manipulating locks, triggers and constraints firing, etc. In general, you want to avoid updating a field to the same value it already has. In English, if plan_id = variable and plan_popular is 0 then set it to 1 but if plan_id is any other value and plan_popular is 1 then set it to 0.
UPDATE {CI}Plans
SET plan_popular = if( plan_id = ?, 1, 0 )
where (plan_id = ? and plan_popular = 0)
or (plan_id <> ? and plan_popular = 1);
The where clause lets through only those rows that will actually be changed by the update. If this is a largish table, that can make quite a difference in response time. Logic is much less expensive than any actual operation that can performed in the database.

MySQL update column which is a value in another column

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) ;

Is it possible to update the cell value only to the specific maximum value?

Is it possible to update the cell value only to the specific maximum value? Here is query:
UPDATE table_1 SET premium_photos = premium_photos + 2 WHERE number = '1234'
I want to limit premium_photos (tinyint) value to max value of 4. Is it possible? For example if premium_photos current value is 2 and query is + 3, then after this query value will be 4.
try
UPDATE table_1 SET
premium_photos = (CASE WHEN (premium_photos + 2) > 4 THEN 4 ELSE (premium_photos + 2) END)
WHERE number = '1234'
you can also use IF function
UPDATE table_1 SET
premium_photos = IF(premium_photos+2>4, 4, premium_photos+2)
WHERE number = '1234'
IF() function documentation