Mysql Trigger with IF and IF ELSE conditions - mysql

i have table 'comments' with fields 'type','receiver_id' ,'confirm' and 'id'.
there are three types of object that each have comment section.
after comment confirm the trigger increase the number of objects in their respective table.
DB::unprepared('CREATE TRIGGER comment_confirm
AFTER UPDATE ON comments FOR EACH ROW
BEGIN
IF OLD.confirm = 0 AND NEW.confirm = 1 THEN
IF OLD.type = profile THEN
UPDATE profiles SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = blog THEN
UPDATE blogs SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = topic THEN
UPDATE topics SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
END IF;
END IF;
END
');
the error for migration is:
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near '' at line 13

Else if should be elseif (no space) IF OLD.type = profile doesn't look right should be in single quotes for a string comparison.

Related

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

Can I use IF in an SQL sentence in MySQL?

UPDATE `order` SET `status` = IF `type` = 2 THEN 1 ELSE 2 END IF;
I wrote this sentence in a MySQL trigger, and I got this error prompt:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type = 2 THEN 1 ELSE 2 END IF at line 5
How can I fix this error?
You're looking for CASE:
UPDATE `order`
SET `status` = CASE WHEN `type` = 2
THEN 1
ELSE 2
END;
You need to use IF() function not IF statement:
UPDATE `order`
SET `status` = IF(`type` = 2, 1, 2);

MySQL Trigger with IF statement returning #1064

I am trying to create a trigger which modifies the field api_key_modified in the table wp_users if an entry in the table wp_bp_xprofile_data with the field_id 106 got modified. The entry should only be updated for the same user.
This is my code:
CREATE TRIGGER trigger_api_key_update AFTER UPDATE ON wp_bp_xprofile_data
FOR EACH ROW
IF new.field_id = 106 THEN
UPDATE wp_users SET api_key_modified = 1 WHERE id = new.user_id;
END IF;
And as a result I get:
1064 - You have an error in your SQL syntax; check the manual that corresponds > to your MySQL server version for the right syntax to use near '' at line 4
The code does work if i delete the IF statement - but then it will update the data for all modifications not just to the ones with field_id 106.
You need to change the delimiter. Otherwise the definition ends at the first ; which would make it incomplete
delimiter |
CREATE TRIGGER trigger_api_key_update AFTER UPDATE ON wp_bp_xprofile_data
FOR EACH ROW BEGIN
IF new.field_id = 106 THEN
UPDATE wp_users SET api_key_modified = 1 WHERE id = new.user_id;
END IF;
END
|
delimiter ;
Without the if the code works because it is a single statement with only one ; at the end.

Keep Getting a Syntax Error when Creating MySQL Trigger

I am trying to Create a Trigger that will fire AFTER Insert of a Record where I will see if there is other records similar to this Inserted Record (Same Date) and if so will update a column in the inserted Record. Once I complete this one I will also update it for AFTER Update as well. Any Help would be Greatly Appreciated.
CREATE
TRIGGER `INSERT_POSTDATEINDEX` AFTER INSERT
ON `zoomloca_listings-dev`.`listings_posts`
FOR EACH ROW
BEGIN
DECLARE vNewPostDateIndex INT;
DECLARE vLastPostDateIndex INT DEFAULT '0';
SET vNewPostDateIndex = '0';
SET vLastPostDateIndex = (SELECT POSTDATEINDEX FROM listings_posts WHERE date(POST_DATE) = date(NEW.POST_DATE) ORDER BY POSTDATEINDEX DESC LIMIT 1);
IF vLastPostDateIndex = '0' THEN
SET vNewPostDateIndex = '0';
ELSE
SET vNewPostDateIndex = vLastPostDateIndex + 1;
END IF;
Update `listings_posts` SET POSTDATEINDEX = vNewPostDateIndex where ID = New.ID;
END
Error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
The problem is that there is no TOP in MySQL. You have to use LIMIT instead. Besides, if you are not using mysql client, then you should remove DELIMITER since it is not a feature of the MySQL. Another thing is that to demarcate the end of an IF statement in MySQL, you should use END IF instead of ENDIF.

creating trigger for Update other table

as a school project i'm making an order and storage system. The server should add the newly arrived items to the storage when the status of an order is changed to received. How do i do this? what i came up with until now is:
CREATE TRIGGER statuswijzigen BEFORE UPDATE
ON `voorraadorder`
FOR EACH ROW
BEGIN
IF NEW.Status = "ontvangen"
THEN UPDATE `voorraadartikel`
SET Voorraad = (SELECT voorraadartikel.Voorraad + voorraadordercontenct.Aantal
FROM `voorraadartikel` AS v
INNER JOIN `vooorraadordercontent` AS r ON v.VoorraadArtikelID = r.ContentID
INNER JOIN `voorraadorder` AS o ON r.VoorraadOrderID = o.VoorraadOrderID
WHERE v.VoorraadArtikelID = r.ContentID AND r.VoorraadOrderID = o.VoorraadOrderID);
END IF;
END
and the error i receive is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 11
all help is welcome :)