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
Related
I have the following script but it returns null all the time.
SELECT
#PRICE_LARGE_PRICE = PRICE_LARGE_PRICE,
#PRICE_SMALL_PRICE = PRICE_SMALL_PRICE
FROM
prices
WHERE
PRICE_LISTING_ID = 60;
SET #ITEM_PRICE = (CASE Size WHEN GivenLargeSizeName THEN #PRICE_LARGE_PRICE
WHEN GivenSmallSizeName THEN #PRICE_SMALL_PRICE
ELSE null
END);
The issue here is
#PRICE_LARGE_PRICE = PRICE_LARGE_PRICE,
#PRICE_SMALL_PRICE = PRICE_SMALL_PRICE
table returns PRICE_LARGE_PRICE & PRICE_SMALL_PRICE correctly but the assignment does not work. Hence CASE fails.
Any help is appreciated.
You need to use SELECT ... INTO:
SELECT PRICE_LARGE_PRICE, PRICE_SMALL_PRICE
INTO #PRICE_LARGE_PRICE, #PRICE_SMALL_PRICE
FROM prices
WHERE PRICE_LISTING_ID = 60;
Note that you need to ensure that the query only returns one row of data, using LIMIT 1 if necessary.
SELECT
#PRICE_LARGE_PRICE:=PRICE_LARGE_PRICE,
#PRICE_SMALL_PRICE:=PRICE_SMALL_PRICE
FROM
prices
WHERE
PRICE_LISTING_ID = 60;
just add colon before equal sign in mysql
I have my database with the following key columns
What I would like to do is using the information provided create 2 new columns that contains a value based on 2 other columns.
1) A 'win' column - This is if 'pos' = 1 then value will be (BSP - 1) - EG One for Billy will read 1.06 else it will read "-1"
2)A 'diditplace' Column - this is if 'Placed' = 1 then value will be (place - 1) - EG One for Billy will read 0.27 else it will read "-1"
Try this:
UPDATE myTable
SET newColumn1 = CASE pos WHEN 1 THEN 'BSCP - 1' ELSE '-1' END,
newColumn2 = CASE placeId WHEN 1 THEN 'place - 1' ELSE '-1' END
If you want to add new columns pragmatically add this before:
ALTER myTable
ADD COLUMN newColumn1 VARCHAR(256) NOT NULL,
ADD COLUMN newColumn2 VARCHAR(256) NOT NULL
Replace newColumn1 and newColumn2 with your desired column names.
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
seems like a stupid question...
I have a mysql table where I want to modify column A to a number 0 or 1 depending on the condition of another column B
So: if( B > 500 ) A = 1 ELSE A = 0
Column A = INT
Column B = DOUBLE
How do you do something like this in sql?
Thanks,
Erik
Try the following statement,
UPDATE tableName
SET A = (B > 500)
SQLFiddle Demo
(B > 500) is a boolean arithmetic in mysql which returns 1 and 0 for true and false , respectively.
You can also use CASE for much more RDBMS friendly,
UPDATE tableName
SET A = CASE WHEN B > 500 THEN 1 ELSE 0 END
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