Add amount in mysql with a certain limit - mysql

I'm using MySQL query
update `table_1`
set `amount` = `amount` + 120
where `id` = 1;
If I run this multiple time will add amount.
Can I set the amount column to a certain amount like 1000?
Means amount will not be added when it comes 1000

Use the LEAST() function to put a cap on the amount.
UPDATE table_1
SET amount = LEAST(amount + 120, 1000)
WHERE id = 1

Related

MAX() in UPDATE query with field does not work

I try to update multiple rows at the same time but amount can have different starting values.
I want to subtract x amount from amount without going below 0. I used UNSIGNED for the amount field in order to protect the number never to fall below 0.
The following query refuses to update all rows that fall below 0 because of the UNSIGNED state:
UPDATE `table`
SET `amount` = `amount` - 35
WHERE `id`
IN (26984, 131881, 985550, 985569, 985586, 1086766, 1189724)
In case amount is for example 12, if you subtract 35, it falls below 0 and refuses to update. But I need it to update to 0.
Then I tried to think of MAX() to help me not fall below 0, but this does not work:
UPDATE `table`
SET `amount` = MAX(`amount` - 35, 0)
WHERE `id`
IN (26984, 131881, 985550, 985569, 985586, 1086766, 1189724)
Any idea how to make this work and to make sure the value never falls below 0?
Thanks, this is how it worked in the end:
UPDATE `table`
SET `amount` = GREATEST(`amount` - 35, 0)
WHERE `id`
IN (26984, 131881, 985550, 985569, 985586, 1086766, 1189724)
And discarding UNSIGNED from amount

MySQL is not using new data

The MariaDB / MySQL event scheduler does not use new data. It only works on data that was in the table at the time of creating the event. How do I make it use whatever is in the table at the time?
CREATE EVENT IF NOT EXISTS my_calculation
ON SCHEDULE EVERY 5 SECOND STARTS '2021-01-01 00:00:00'
DO
SET #c = (SELECT COUNT(*) FROM my_table);
SET #rownum = 0;
UPDATE my_table
SET rank = (100 / #c) * (#rownum:= 1 + #rownum)
ORDER BY another_column DESC LIMIT 100000;
its because in your event query you have this line :
ORDER BY progress DESC LIMIT 100000;
So it always process only 100000 first rows that are ordered by progress column , remove LIMIT 100000 and you will be fine.

If one column value is >= 1/3 of another column value input text into another column

I have a table called membership. One column is called amount_paid and the other is called valid_membership.
I have another table called m_type with a column called price.
The linking clause is WHERE membership.type_id = m_type.type_id
I want a trigger before insert on the membership table to check if amount paid is >= 1/2 of price. If it is greater i want 1 to be placed in valid_membership else if it isn't true then 0 to be placed in valid_membership.
Just having a little trouble with the correct syntax,
This is what i have tried already--
DELIMITER
//
CREATE TRIGGER valid_membership
BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
IF (NEW.amount_paid >= 1/2 price) THEN
SET valid_membership = '1' ELSE '0'
WHERE membership.type_id = m_type.type_id
END IF;
END
//
DELIMITER ;
Thanks
1/3 price should coded as price / 3, but even better you could use plain SQL:
update membership set
valid_membership = (
select amount_paid
from price
where membership.type_id = m_type.type_id
) >= price / 3
which can be executed as a plain update query, to both test the query and also backfill existing data.
For the trigger:
set new.valid_membership = (
select amount_paid
from price
where new.type_id = m_type.type_id
) >= price / 3
This does use a small “trick” special to MySQL: In MySQL, true is 1 and false is 0.

Minus the values of 2 specific rows in MySQL

Im working on calculating through MySQL. The next values are day values and now I want to see what the difference is between the last row and 1 day (= 96 quarters) earlier. The line below is for the last and newest value:
SET #now = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 1);
The next line is the value of 1 day ago. This is the same as 96 quarters.
SET #yesterday = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 95,1);
Now I want to minus the newest value with the value 1 day ago so I know the difference between those values. The code below is what ive created right now:
CREATE EVENT Value_Test
ON SCHEDULE EVERY 15 MINUTE
STARTS '2017-08-20 16:36:00'
DO
SET #now = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 1);
SET #yesterday = (SELECT * FROM solar.measurements WHERE `tag` LIKE '%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC LIMIT 95,1);
SET #value = (#now - #yesterday);
INSERT INTO solar.measurements (nad, tag, value, timestamp) VALUES (18223, 'c18223.ValueDayTest', #waarde, CURRENT_TIMESTAMP());
When I want to execute this I get the following error: Error Code: 1048. Column 'value' cannot be null. Can someone help me by finding a solution? Thanks!
EDIT:
Answer to a question
Day Ago
Today
A SHOW CREATE EVENT Value_Test would show you that your event wasn't done properly.
Try this:
Delimiter //
CREATE EVENT Value_Test
ON SCHEDULE EVERY 15 MINUTE
STARTS '2018-08-20 16:36:00'
DO
BEGIN
SET #now = (SELECT value FROM solar.measurements WHERE `tag` LIKE
'%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC
LIMIT 1);
SET #yesterday = (SELECT value FROM solar.measurements WHERE `tag` LIKE
'%18223.Inepro_Total_Forward_kWh[3]%' ORDER BY `measurements`.`timestamp` DESC
LIMIT 95,1);
SET #value = (#now - #yesterday);
INSERT INTO solar.measurements (nad, tag, value, timestamp) VALUES (18223,
'c18223.ValueDayTest', #waarde, CURRENT_TIMESTAMP());
END
//
Delimiter ;
In case of the need to use compound statements inside an EVENT you need to put them under Begin/End

update positive to negative value in mysql

i have payment table fields
update reason and amount & total field are change negative
UPDATE payment
SET reason = 'refund'
WHERE uid =5 AND date = '2012-05-01' AND accid =2
update single query is it possible?
If I understand you correctly, you also want to set amount column to positive value along with the above statement.
You can use something like this
UPDATE payment
SET reason = 'refund', amount = amount * -1, total = total * -1
WHERE uid =5 AND date = '2012-05-01' AND accid =2
Use ABS(amount) if you wish to always get the positive integer.
SELECT ABS(5);
will output 5
SELECT ABS(-5);
will also output 5
When I looked for the solution, the offered suggestion corrupted my result:
SELECT #TotalAmount:=( SELECT FORMAT(SUM(Amount), 4) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
Proper result:
After formatting:
SELECT #TotalAmount:=( SELECT FORMAT(SUM(Amount), 4) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
SELECT #TotalAmount * -1;
Probably doesn't work well with formatting.
Another solution is to subtract your digit from zero:
SELECT #TotalAmount:=( SELECT SUM(Amount) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
select FORMAT((0 - #TotalAmount), 4 );
To avoid the corruption of the the result I described above, I make formatting at the end of the operation. The result is fine then:
Works also with multiplication by -1:
SELECT #TotalAmount:=( SELECT SUM(Amount) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
select FORMAT(( #TotalAmount *-1), 4 );