MySQL said: Error #1064 - while using IF condition in sql - mysql

I got this error msg in my sql command. i am unable to figure out the cause. please help me.
if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then
begin
UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1'
end ;
else
begin
INSERT INTO `teams`(`client_id`, `member_id`) VALUES ('3','33')
end;
end if;
here i'm trying to update column current if set to 1, else insert a new record.
I need to use this sql command in my php file when a form is submitted.
below is the error message:
#1064 - You have an error in your SQL syntax;

This is not a valid statement in mysql, the valid syntax would be as below. Also if-else is allowed in stored procedure, functions or triggers.
if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then
begin
UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1'
end ;
else
begin
INSERT INTO `teams`(`client_id`, `member_id`) VALUES ('3','33')
end;
end if;

The whole set of statements can be done with one INSERT...ON DUPLICATE KEY UPDATE... statement.

Related

How to get value from select in trigger mysql and use it for condition of IF

DELIMITER $$
CREATE TRIGGER cek
AFTER INSERT ON lapor_karya
FOR EACH ROW
BEGIN
SET #var = (SELECT COUNT(lapor_karya.ID_Lapor) FROM lapor_karya GROUP BY NEW.ID_Karya);
IF(#var > 10)
THEN
DELETE * FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END $$
DELIMITER ;
error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END' at line 9
I want to get value from lapor_karya table, and use this value for condition IF. If my value more than 10, then will execute query delete. But it's doesn't working
The error which you got about the DELETE statement. You have to leave * when you writing DELETE statement.
Also, just try without parentheses.
IF #var > 10 THEN
DELETE FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
For more info, check this out MySQL documentation.
A slight simplification:
BEGIN
IF ( SELECT COUNT(*) FROM lapor_karya
WHERE ??? = NEW.ID_Karya
) > 10
THEN
DELETE * FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END $$
That is, a parenthesized SELECT that returns a single value can be used virtually any place where an expression can be used.

MySQL syntax Error on Create Trigger

Here is my trigger, I am getting the MySQL syntax error. I wanted to reduce the balance from sms_index table sms_count column value.
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count) WHERE group_id = OLD.ins_group_id;
END IF;
END;
Error 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 '' at line 5
Your code looks correct, except for the possible problem of the delimiter. Try the following:
DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count)
WHERE group_id = OLD.ins_group_id;
END IF;
END;//
DELIMITER ;
From the documentation:
However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition.

Create trigger syntax error while inserting for trigger action

I am trying to create a trigger such that if there is any change in a particular table column then event should be created in another. I am using following sql query for this
CREATE TRIGGER enabled_update BEFORE UPDATE ON useraccount
FOR EACH ROW
BEGIN
IF (NEW.enabledAccount != OLD.enabledAccount) THEN
INSERT INTO logEvents
SET NEW.event = OLD.employeeID;
END IF;
END;
But I am getting following error when I am trying to run this query.
Error in query (1064): Syntax error near '' at line 6 and Error in query (1064): Syntax error near 'END IF' at line 1. Where I am mistake?
Use the DELIMITER command, see 23.1 Defining Stored Programs.
DROP TRIGGER IF EXISTS `enabled_update`;
DELIMITER //
CREATE TRIGGER `enabled_update`
BEFORE UPDATE ON `useraccount`
FOR EACH ROW
BEGIN
IF (NEW.`enabledAccount` != OLD.`enabledAccount`) THEN
INSERT INTO `logEvents`
SET `event` = OLD.`employeeID`;
END IF;
END//
DELIMITER ;

Error 1064 on Mysql trigger

I'm writing a trigger in MySQL to take log of table updates. The log table is called individuo_storico and the target table is called individuo. When individuo is updated, I want to check if IDQualifica and IDLivello are changed, if yes a record in individuo_storico is inserted.
I write down this code but I get a #1064 error, where's the syntax error?
use ore;
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
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
Please wrap your trigger creation with the monikers necessary so the db engine does not choke on it. So three areas:
1) line 1
2) right after the end;
3) and then a reset to a default delimiter.
Same concept is typical with stored proc creations.
DELIMITER $$
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;$$
DELIMITER ;

sql syntax error creating trigger

I want to create trigger and I have written this query but this does not execute. Please check my query
CREATE
TRIGGER 'blog_after_insert' AFTER INSERT
ON 'blog'
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, #changetype);
I am getting this 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 ''blog_after_insert' AFTER INSERT
ON 'blog'
FOR EACH ROW BEGIN
IF NEW.del' at line 2
Please run this query
DELIMITER $$
CREATE
TRIGGER blog_after_insert AFTER INSERT
ON blog
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = "DELETE";
ELSE
SET #changetype = "NEW";
END IF;
INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, #changetype);
END$$
DELIMITER ;
Single quotes (') denote string literals - object names, like triggers and tables should use forward quotes, or no quotes at all:
CREATE
TRIGGER blog_after_insert AFTER INSERT -- here
ON blog -- and here
FOR EACH ROW BEGIN
-- rest of code...