MySQL Trigger with conditional statement and update another table - mysql

On MySQL 5.5, I am trying to write a trigger to do the following:
Checks a condition for the insert (is group_id between 8-20?)
If the condition is true, then update the user with the same user_id on another table, called phpbb_users.
The update on this other table is to set user_rank to NEW.group_id from the insert on the table that has the trigger.
Else, do nothing.
Not experienced at all, and stuck. It's not working when I run the query. Here is what I've got so far.
DELIMITER $$
CREATE OR REPLACE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW BEGIN
IF (NEW.group_id BETWEEN 8 AND 20) THEN
UPDATE phpbb_users
SET user_rank=NEW.group_id
WHERE user_id=NEW.user_id;
ELSE
UPDATE phpbb_users
SET user_rank='0'
WHERE user_id=NEW.user_id;
END IF;
END$$;
DELIMITER;
(Note, the "else" clause is unnecessary really - happy for any suggestions for improvement of this code!)

if you only want to get rid of the else try
CREATE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW
UPDATE phpbb_users
SET user_rank=IF(NEW.group_id BETWEEN 8 AND 20,NEW.group_id,"0")
WHERE user_id=NEW.user_id;

Related

unable to update the record in triggered table when new record is found in mysql trigger

unable to write a trigger to insert a row if no imei found in the triggered table if found then it should update the record.
query error - lost connection to my sql server.
DELIMITER $$
create TRIGGER update_livedata4
AFTER INSERT ON `Rawdata`
FOR EACH ROW
begin
IF Livedata.IMEI <> Rawdata.IMEI THEN
INSERT INTO Livedata(IMEI,updatedTime,latitude,longitude,speed,ignition,fuel,altitude,battery,runHrs,alert,distance) VALUES (NEW.IMEI,NEW.updatedTime, NEW.latitude,NEW. longitude,NEW.speed,NEW.ignition,NEW.fuel,NEW.altitude,NEW.battery,NEW.runHrs,NEW.alert,NEW.distance);
END IF;
UPDATE Livedata SET IMEI = new.IMEI,updatedTime=new.updatedTime,latitude= NEW.latitude,longitude=NEW. longitude,speed=NEW.speed,ignition=NEW.ignition,fuel=NEW.fuel,altitude=NEW.altitude,battery=NEW.battery,runHrs=NEW.runHrs,alert=NEW.alert,distance=NEW.distance;
END$$
DELIMITER ;
It seems that you are missing where clause in the update that you have written which in turn takes more than your net read timeout....!
Are you willing to update all the records?
If that is the case then increasing the timeout would help...

mysql trigger - is there anything like FOR EACH TABLE

MySql trigger is very interesting, but very tricky.
I have some problem, I want to run a trigger once after insert on .
I want to run my trigger once after rows inserted, is there anything like for each table``???
how to make this trigger run only once, but not for each row created.
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mydb`.`leave_taken_trigger`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
set #lt:= (SELECT count(*) FROM `leave` where (staff_leave_application_staff_id_staff = new.staff_leave_application_staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type) and active = 1 );
INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, taken_days, updated)
VALUES (new.leave_type_id_leave_type, new.staff_leave_application_staff_id_staff, IFNULL(#lt,0), CURDATE())
ON DUPLICATE KEY UPDATE taken_days=IFNULL(#lt,0), updated = CURDATE();
END$$
I think you can't achieve your requirement with trigger.
Your trigger will run in every row created.
I suggest you to using stored procedure from your code after the INSERT has successfully completed.

MYSQL Stored Proc: How to delete a just updated record

I have two table Name: registeredList & deregisteredlist. Now when a user getting getting deregistered from "registeredlist" table then a trigger update his info to deregistration table and delete the record from registration table. On my below proc i can update it properly but can't delete the user from registered table. My proc:
DELIMITER $$
USE `abc_db`$$
DROP TRIGGER `UnsubscriberListTrigger`$$
CREATE
TRIGGER `UnsubscriberListTrigger` AFTER UPDATE ON `registeredlist` FOR EACH ROW
BEGIN
IF (old.SubscriberStatus='registered') THEN
INSERT INTO deregisteredlist(name,SubscriberStatus,DeRegistrationDate)
VALUES(old.name,'Deregistered',NOW());
DELETE from registeredlist where old.id=new.id;/???????/I am getting problem here
END IF;
END $$
DELIMITER ;
Thanks in advance.
I think all you need is to change WHERE in DELETE statement.
It should go like this:
DELETE from registeredlist where id=old.id; // (or new.id cause in this case old.id is equal to new.id)
... because you want to match it against id column.
UPDATE:
Another possibility is this:
- create AFTER INSERT TRIGGER on deregisteredlist which will do the DELETE in registeredlist. That way you shouldn't get that error.
Try this::
DELETE from registeredlist order by updateddat desc limit 1;

Using columns from another table in mysql if statement

I wrote a trigger something like this:
CREATE TRIGGER `update_after_itemPresent` AFTER INSERT ON `bus_repair`
FOR EACH ROW begin
IF NEW.unit <> `item_present`.`unit` THEN
update item_present
set unit = unit-new.unit
where item_present.item_group_id = new.item_group_id;
END IF;
end
But when I insert new row in bus_repair table it gives an error that:
unknown table item_present in field list
Any idea how to fix this?
Move your UPDATE item_preset statement above the IF and the IF inside the UPDATE or rephrase as a condition. You need to UPDATE or SELECT the table item_present first.

How can I get an after update trigger to work when it is being fired from an after insert trigger in mysql?

Hello, every one :)!
I'll try and keep this as simple as possible, basically, I have one table that references itself via a parent_id column. Each row in the table can have a parent and can keep count of how many children it has via the count column. So essentially what I'm trying to do is have the triggers update each parent row's count column when necessary
The problem is that the update trigger gets called when the update operation in the insert trigger gets called. Then I get:
"General error: 1442 Can't update table 'term_taxonomies' in stored function/trigger because it is already used by statement which invoked this stored function/trigger".
Any ideas?
Actual code:
TRIGGER `dbname`.`ai_term_taxonomies`
AFTER INSERT ON `dbname`.`term_taxonomies`
FOR EACH ROW
BEGIN
IF NEW.parent_id NOT 0 THEN
UPDATE term_taxonomies as termTax SET assocItemCount = (assocItemCount + 1)
WHERE termTax.term_taxonomy_id = NEW.parent_id;
END IF;
END$$
CREATE
TRIGGER `dbname`.`au_term_taxonomies`
AFTER UPDATE ON `dbname`.`term_taxonomies`
FOR EACH ROW
BEGIN
IF NEW.parent_id NOT OLD.parent_id THEN
IF NEW.parent_id NOT 0 THEN
UPDATE term_taxonomies as termTax SET assocItemCount = (assocItemCount + 1)
WHERE termTax.term_taxonomy_id = NEW.parent_id;
END IF;
IF OLD.parent_id NOT 0 THEN
UPDATE term_taxonomies as termTax SET assocItemCount = (assocItemCount - 1)
WHERE termTax.term_taxonomy_id = OLD.parent_id;
END IF;
END IF;
END$$
All mysql triggers execute in the same transaction as the triggering statement.
You want to update using the SET NEW.assocItemCount syntax as opposed to performing an UPDATE statement on the underlying table.
Edit: However, in your case this is not possible because you are updating a different row in the same table, the hardest thing to do in a mysql trigger. Sorry.
You will have to change your schema. Take assocItemCount out of your table, create a new table holding just term_taxonomy_id and assocItemCount, and update that using an UPDATE statement from your query. It is also possible to use a view joining these two tables to hide this detail if a query needs to use your original schema.
Alternatively, if you did not have assocItemCount in your database at all, you would still be able to compute it in any queries, and your database would be better normalized than it is now.