Phpmyadmin gives meaningless syntax error for trigger - mysql

I'm trying to add a trigger to my database however I get an "not meaningful" syntax error from mysql.
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 5
CREATE TRIGGER update_balance_table AFTER INSERT ON _explorer.transfers
FOR EACH ROW
BEGIN
/* TO*/
INSERT INTO _explorer.balances (account, balance) VALUES (new.account, new._to) ON DUPLICATE KEY UPDATE balance = balance + new._quantity;
/* FROM*/
UPDATE _explorer.balances SET balance = balance - new._quantity WHERE account = new._from;
END

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 ;

triggers in mysql getting an error

I'm trying to create a trigger that will update some value, and if the row is not exists than insert a new row with value = 1.
I wrote the trigger in 3 possible ways and all of them are getting error
version 1:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics
SET item_id = NEW.item_id,
likes = 0,
comment = 1
ON DUPLICATE KEY UPDATE
SET comment = OLD.comment + 1;
END;
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 'SET comment = OLD.comment + 1' at line 9
version 2:
CREATE TRIGGER stat_trg AFTER INSERT ON comments
FOR EACH ROW
BEGIN
DECLARE num integer DEFAULT 0;
SELECT comment into num FROM statistics
WHERE item_id = OLD.item_id;
SET num := num + 1;
INSERT INTO statistics (item_id, comment, likes) VALUES (num,1,0)
END;
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
version 3:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics (item_id, likes, COMMENT) VALUES (NEW.item_id,0,1)
ON DUPLICATE KEY UPDATE
comment = comment + 1;
END;
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
All looks good according to examples that I found, what am I missing?
Please help on this issue.
Thanks.
Please try this:
DELIMITER $$
CREATE TRIGGER `stat_trg` BEFORE INSERT ON `comments`
FOR EACH ROW BEGIN
INSERT INTO `statistics` set item_id= NEW.item_id, likes = '0', comments = '1' ON DUPLICATE KEY UPDATE
comments = values(comments) + 1;
END$$
DELIMITER ;

MySQL triggers on the same table

I have some tables. If I update st_income column, then I want to update total_income column, and when I do this I want update next column (income_per_person). I tried this:
IF (NEW.st_income <> OLD.st_income) THEN
SET NEW.total_income=OLD.total_income + (NEW.st_income-OLD.st_income);
END IF
IF(NEW.total_income <> OLD.total_income) THEN
SET NEW.income_per_person=NEW.total_income/(family_mem_numbers+1)/12;
END IF
but it doesn't work.
EDIT
This is return message:
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 'IF(NEW.total_income <> OLD.total_income) THEN SET NEW.income_per_person=NEW' at line

mysql stored procedure declare error

I don't really get the error ...
i put the declare statement right after begin. i usually do a lot of MS SQL so i am not that practiced in mysql syntax. please help me out
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddEntry2`(IN `inDate` DATE, IN `inUser` INT, IN `inComment` TEXT, IN `inStart` INT, IN `inEnd` INT)
BEGIN
DECLARE commID UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT commID := MAX(id)
FROM dimcomment
WHERE comment = inComment;
INSERT INTO factentry (FKDate, FKUser, FKComment,FKStart,FKEnd,FKAbsence)
VALUES (
inDate,
inUser,
commID,
inStart,
inEnd,
1
);
END //
Error 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 'UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT c' at line 3
well i changed UNSIGNED to INT and Error Message changed:
#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 ':= MAX(id)
FROM dimcomment
' at line 5
Seems like i cannot set via SELECT VAR := VALUE ?

Error in my stored-procedure

i have an error in my stored-procedure. I use MySql DB
SET #counter = 1;
SET #last = 0;
UPDATE Customer SET ordre = (IF(#last = customer_id,#counter + 1,#counter = 1)),
#last = customer_id
My Error
Script line: 3 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 '#last = customer_id ORDER BY
customer_id' at line 2
You cannot set variables in SET clause of UPDATE statement. '#last = customer_id' causes the error.
From the reference -
UPDATE syntax - '...SET col_name1=expr1 [, col_name2=expr2 ...]'
The SET clause indicates which columns to modify and the values they should be given.