Trigger Update - 2 Tables - mysql

I need a trigger for when I Fize hum insert into the table " reserve" automatically add in Table Room The Current State .
A major issue is that I need to check if reserva.id E equal to quarto.id .
How can I check the condition WHERE The Field And Correspondent ?
Here's Some failed attempts codes .
CREATE TRIGGER OcuparQuarto
AFTER UPDATE OF id ON reserva
FOR EACH ROW
BEGIN
UPDATE quarto
SET status='0'
WHERE id=NEW.id;
END;
or
Delimiter |
create trigger OcuparQuarto
after insert on reserva
for each ROW
UPDATE quarto
SET quarto.status = 0
FROM quarto
INNER JOIN reserva ON quarto.id = reserva.id
END;
|
delimiter ;

begin
if (new.status = 0) then update quarto set status = 0 where quarto.id
= new.quarto_id;
elseif (new.status = 1) then update quarto set status = 1 where
quarto.id = new.quarto_id;
elseif (new.status = 2) then update quarto set status = 0 where
quarto.id = new.quarto_id;
elseif (new.status = 3) then update quarto set status = 1 where
quarto.id = new.quarto_id;
end if; end
This Works! Thanks!

Related

Concatenate old value with new one

I have a note table and a propal table and my trigger allows after insertion to write what is in my "note_value" field of the note table in my note_public field of my propal table or of my facture table. however the trigger overwrites the value of note_public each time, which I don't want. I would like it to be added, so that it is concatenated.[image of note table][1]
[image of propal table][2]
(when the visibility is 1 this a public note)
BEGIN
IF (NEW.visibility = 1) THEN
IF (NEW.item_type = 'propal') THEN
UPDATE anas01_propal SET note_public = new.note_value WHERE(rowid = new.item_id) ;
ELSEIF (NEW.item_type = 'facture') THEN
UPDATE anas01_facture SET note_public = new.note_value WHERE(rowid = new.item_id) ;
END IF;
END IF;
END
[1]: https://i.stack.imgur.com/JmbQA.png
[2]: https://i.stack.imgur.com/P2Ct8.png

Update Data When Trigger Execute in Mysql?

I want WaterRateMeterStart with WaterRateMeterEnd have same value sequentially if data updated.
This is my table
And This is my table structure
This my code and
I do not know how mistakes can occur.
DELIMITER $$
CREATE TRIGGER after_update_rates_water_again AFTER UPDATE ON waterrates FOR EACH ROW
BEGIN
IF
new.WaterRateMeterStart <> old.WaterRateMeterStart THEN
UPDATE waterrates
SET WaterRateMeterEnd = new.WaterRateMeterStart
WHERE
WaterRateId = ( new.WaterRateId - 1 );
ELSEIF new.WaterRateMeterEnd <> old.WaterRateMeterEnd THEN
UPDATE waterrates
SET WaterRateMeterStart = new.WaterRateMeterEnd
WHERE
WaterRateId = ( new.WaterRateId + 1 );
END IF;
END $$
DELIMITER ;
And error if I updated the data
UPDATE `waterrates` SET `WaterRateMeterStart` = '9' WHERE `waterrates`.`WaterRateId` = 2

generated column with condition sql

mysql command to generate a column from other column according to condition
loan_amount | installment | start_date | status
------------------------------------------------------------------
500 | 100 | 2018-1-1 |
if (loan_amount % installment != 0) then month_required to pay = loan_amount / installment + 1
else month_required = loan_amount / installment;
then i want to check by adding month_required to start_date if it has crossed current date. If not then status would generate "incomplete" else "complete" .
delimiter //
set #month = "0";
create trigger `status_check` before insert on `loan`
for each row
begin
if NEW.loan_amount% NEW.installment != 0
then set #month NEW.loan_amount/ NEW.installment +1;
else set #month = NEW.loan_amount/NEW.installment;
end if ;
if NOW() <= dateadd(NEW.start_date,INTERVAL #month MONTH)
then set NEW.status = "incomplete";
else set NEW.status = "complete";
end if;
end;
//
DELIMITER ;
what is the error here? please help.
I Can't make the real answer as you don't provide all your database field nor a data sample, but some BEFORE INSERT and BEFORE UPDATE Trigger will probably do the job.
This is the base for the BEFORE INSERT :
DELIMITER //
DROP TRIGGER IF EXISTS `trg_test_insert`;
//
CREATE TRIGGER `trg_test_insert`
BEFORE INSERT ON `table`
FOR EACH ROW
BEGIN
IF NEW.loan_amount % NEW.installment != 0 THEN
SET NEW.month_required = NEW.loan_amount / NEW.installment + 1;
ELSE
SET NEW.month_required = NEW.loan_amount / NEW.installment;
END IF;
END;
//
DELIMITER ;
Take a look at MySQL 5.7 Reference Manual / ... / Trigger Syntax and Examples
And at :
MYSQL Triggers - how to store the result of a calculated field

trigger mysql unknown table

I've been trying to solve this problem. Here is the code:
CREATE TRIGGER some_trigger AFTER UPDATE ON table_a
FOR EACH ROW BEGIN
DECLARE tname VARCHAR(20);
IF (table_a.field_offer=1) THEN
SET tname='table_b';
ELSEIF (table_a.field_offer=0) THEN
SET tname='table_c';
END IF;
IF ((new.field_state = 0)) THEN
UPDATE tname join table_a on tname.ID=table_a.ref_field
SET tname.STOCK = tname.STOCK -1;
ELSEIF ((new.field_state = 1)) THEN
UPDATE tname join table_a on tname.ID=table_a.ref_field
SET tname.STOCK = tname.STOCK +1;
END IF;
END;
I am getting:
Unknown table 'table_a' in field list. Field_offer and field_state can
be null.
I've shown below what was said in the comments to the question:
CREATE TRIGGER some_trigger AFTER UPDATE ON table_a
FOR EACH ROW BEGIN
DECLARE tname VARCHAR(20);
IF (NEW.field_offer=1) THEN
UPDATE `table_b`
SET STOCK = CASE NEW.field_state
WHEN 0 THEN STOCK - 1
WHEN 1 THEN STOCK + 1
ELSE STOCK
END
WHERE ID=NEW.ref_field
;
ELSEIF (NEW.field_offer=0) THEN
UPDATE `table_c`
SET STOCK = CASE NEW.field_state
WHEN 0 THEN STOCK - 1
WHEN 1 THEN STOCK + 1
ELSE STOCK
END
WHERE ID=NEW.ref_field
;
END IF;
...
Note that I changed the updates from UPDATE ... JOIN as MySQL does not allow you to change the data of the triggered table; while you were not actually updating table_a, the JOIN could have been enough for MySQL to have objected... that and the joins would've updated every row in table_b/c that had a match in table_a, not just table_a rows related to the values in or row of the trigger.

Why doesn't this trigger work the way I want it to?

I have two tables in my mysql database:
Advisor: id
Student: advisorID | major
I want to update Student where the deleted ID in Advisor is equal to advisorID and use a set of if statements to change that student's advisorID value based on that student's major. This is what I have thus far:
delimiter //
create trigger advisor_delete after delete
on advisor
UPDATE Student
IF Student.major = 'major1' THEN SET Student.advisorID = 1;
ELSEIF Student.major = 'major2' THEN SET Student.advisorID = 2;
ELSEIF Student.major = 'major3' THEN SET Student.advisorID = 3;
ELSE SET Student.advisorID = 4;
ENDIF;
WHERE Student.advisorID = OLD.id;
end//
Any help is welcome. Thanks.
I don't know this UPDATE/IF ELSEIF syntax, but CASE statement will work for you:
delimiter //
create trigger advisor_delete after delete
on advisor
UPDATE Student
SET Student.advisorID =
CASE Student.major WHEN 'major1' THEN 1
WHEN 'major2' THEN 2
WHEN 'major3' THEN 3
ELSE 4
END
WHERE Student.advisorID = OLD.id;
end//