I have a variable in my game that adds points and sometimes substract points.
In my database I have a score table but the value of the score must always be between 0 and 20.
So if my score is currently 2 and the variable $value is -3 I can avoid to go under 0 by doing this.
UPDATE table
SET field = GREATEST(0, field + $value)
WHERE id = $id
Is there a way so that the value won't go under 0 and not above 20?
You can use a case expression.
UPDATE [table]
SET [field] = CASE
WHEN [field] + #value > 20
THEN 20
WHEN [field] + #value < 0
THEN 0
ELSE [field] + #value
END
WHERE [id] = #ID;
You can nest GREATEST and LEAST like this to achieve what you want.
UPDATE table
SET field = GREATEST(0, LEAST(20, field + $value))
WHERE id = $id
Related
I have a table UnitCheck:
enter code here
I want to add a new column which will return me a value Greatest if the CostToRestaff value is the highest and should a return a value Not Greatest for every other value of`enter code here CostToRestaff.
CostToRestaff is a calculated column with the formula: 0 * FullDutyC + 10 * WoundedC + 25 * KilledC;
enter code here
edited:
I was trying more on these lines. Any idea where i am going wrong?
''''''
if Unit_ID = A then --A is the user parameter for my procedure
Set check1 = 0 * FullDutyC + 10 * WoundedC + 25 * KilledC;
end if;
if Unit_ID = B then --B is the user parameter for my procedure
Set check2 = 0 * FullDutyC + 10 * WoundedC + 25 * KilledC;
end if;
if Unit_ID = C then --C is the user parameter for my procedure
Set check3 = 0 * FullDutyC + 10 * WoundedC + 25 * KilledC;
end if;
if check1 > check2 AND check1 > check3 then
set Rejection = /* this is where i need the final answer which should be
the value itself for eg: 125 - since it is the highest value*/ where Unit_ID =
A;
elseif check2 > check3 AND check2 > check1 then
Set Rejection = /* this is where i need the final answer in this case since it is not the highest value then it should jump to the else condition*/ where
Unit_ID = B;
elseif check3 > check1 AND check3 > check2 then
Set Rejection = /* this is where i need the final answer in this case since it is not the highest value then it should jump to the else condition*/ where Unit_ID =
C;
else set Rejection = "Not Greatest"
end if;
''''''
Assuming MySQL 8.0, you can use window functions:
select
t.*,
(rank() over(order by CostToRestaff desc) = 1) isGreatest
from mytable t
This gives you a 0/1 flag that indicates if the current is has the greatest value of CostToRestaff across the whole table - which I find more meaningful that string '(Not) Greatest'. If there are top ties, they would all get the 1 flag.
If you want that as a string value:
select
t.*,
case when rank() over(order by CostToRestaff desc) = 1
then 'Greatest'
else 'Not Greatest'
end isGreatest
from mytable t
In earlier versions, you can use a subquery or a join to compute the maximum value:
select
t.*,
(select max(CostToRestaff) from mytable) = CostToRestaff isGreatest
from mytable t
I know how to increase/decrease value in column.
value type is INT(11)
Command:
UPDATE table SET value = value - 1 WHERE id = 1
But when value reaches "0" it keeps decreasing.
Question: How to decrease until zero, so I don't get value (-1) in column?
Thank you for your code / ideas / suggestions
Just use a conditional:
UPDATE table
SET value = value - 1
WHERE id = 1 AND value > 0;
Add it to the where clause:
UPDATE table SET value = value - 1 WHERE (id = 1) AND (value > 0)
Once value reaches 0, it won't get updated anymore, because the where clause will exclude that record from the update.
You can add on to your where clause like so:
UPDATE table
SET value = value - 1
WHERE id = 1 and value >= 1
You can use CASE.
UPDATE table SET value = CASE WHEN value = 1 THEN value = value - 1
ELSE value
END
WHERE id = 1
Update myTable SET field = 1 (if field = 0 or if field is null) where myid = 12345;
Update myTable SET field = 0 (if field = 1) where myid = 12345;
What is the best way to transform this Pseudocode in proper SQL for Oracle and MySQL?
You could simply use the modulo like this:
UPDATE myTable SET field = (field + 1) % 2 WHERE myId = 12345;
Due to the lack of a real boolean in both DBMS you need a case statement:
update myTable
set the_column = case when the_column = 1 then 0 else 1 end
where myId = 12345;
This assumes that the column never has different values than 0 and 1
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
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 .