Mysql triggers and insert values - php

I try to set a trigger before I insert a value into table ec3_checking, the trigger's function is set 'account_id' to 'CH-n' if the highest value for id_count is (n-1), id_count is an auto_increment parameter.
delimiter //
CREATE TRIGGER account_id_add BEFORE INSERT ON `ec3_checking`
FOR EACH ROW
BEGIN
DECLARE anum int;
DECLARE bnum int;
SET bnum = max(id_account);
SET anum = bnum + 1;
SET NEW.account_id = concat('CH-', anum);
END//
Then I try to insert some values into the table
insert into `ec3_checking` (balance, overdraft_limit)
VALUES
('300','50');
There is an error:1111, invalid use of group function, could anyone help me?
Thank you very much.

CREATE TRIGGER `account_id_add` BEFORE INSERT ON `ec3_checking`
FOR EACH ROW BEGIN
DECLARE anum int;
SET #bnum = (SELECT max(id_account) FROM ec3_checking);
SET anum = #bnum + 1;
SET NEW.account_name = concat('CH-', anum);
END

Related

How to get last inserted row ID from wordpress database with trigger which increment ID

I use Wordpress with a database. My problem is that I want to retrieve the inserted ID when using wpdb-> insert. I want to clarify that I increment the ID using a database trigger because my table is a relative entity. I was thinking of creating a procedure that I would call after my insertion.
My trigger
BEGIN
DECLARE num INTEGER;
IF NEW.id IS NULL THEN
SET num =
(
SELECT MAX(id) + 1
FROM ab_autreFrais
WHERE idDevis = NEW.idDevis
);
SET NEW.id = num;
ELSEIF NEW.id = 0 THEN
SET num =
(
SELECT MAX(id) + 1
FROM ab_autreFrais
WHERE idDevis = NEW.idDevis
);
SET NEW.id = num;
END IF;
END
There is the LAST_INSERT_ID() function that you can use instead of the trigger. Also you can get this value through an output parameter of a stored procrdure.
Or you could use auxiliary table or a global variable to put id value from the trigger. For example:
...
SET NEW.id = num;
UPDATE `SysData` SET `LastID` = num;
...
#Alexander
UPDATE ``SysData`` SET ``LastID`` = num; Does not work on MySQL SysData is not exist. I used SET SESSION last_insert_id = num in sql on phpmyadmin it work but with wpdb when i call last_insert_id() it return 0

#1054 Unknown Column in Field List in mysql Procedure

I have table name "tmp1" which has fields name id and comma. comma contains 1,3,2,4,5 values with text datatype.
I want separate value in loop in my stored procedure. Like 1 then 3 then 2 etc. So, I was create below given procedure.
BEGIN
DECLARE my_delimiter CHAR(1);
DECLARE split_comma text;
DECLARE done INT;
DECLARE occurance INT;
DECLARE i INT;
DECLARE id INT;
DECLARE sel_query VARCHAR(500);
DECLARE splitter_cur CURSOR FOR SELECT id,comma from tmp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN splitter_cur;
splitter_loop:LOOP
FETCH splitter_cur INTO id,split_comma;
SET occurance = LENGTH(split_comma) - LENGTH(REPLACE(split_comma,',',''))+1;
SET my_delimiter=',';
IF done = 1 THEN
LEAVE splitter_loop;
END IF;
IF occurance > 0 THEN
SET i = 1;
WHILE i <= occurance DO
SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1";
SET #sel_query = sel_query;
PREPARE sel_query FROM #sel_query;
EXECUTE sel_query;
SET i = i + 1;
END WHILE;
END IF;
SET occurance = 0;
END LOOP;
CLOSE splitter_cur;
END;
But when I execute this procedure error occurs "MySQL said: #1054 - Unknown column 'i' in 'field list'"
But here i is a variable used to rotate loop.
Is there any solution? Please help me ... Thank you in advance ...
You have to embed value of variable i in the query, which you did not do.
Change:
SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1";
SET #sel_query = sel_query;
To:
SET #sel_query = CONCAT( "SELECT SUBSTRING_INDEX(comma,',',", i, ") as abc from tmp1 )";

MySQL Stored Procedure running slow

I have created the following stored proc in MySQL:
BEGIN
DECLARE temp_mold VARCHAR(30);
DECLARE temp_time DATETIME;
DECLARE temp_shot_ct INT;
DECLARE temp_shot_count INT;
DECLARE temp_shot_mold VARCHAR(30);
DECLARE temp_shot_cavity_count INT;
DECLARE temp_job_count INT;
DECLARE temp_row_count INT;
DECLARE done INT DEFAULT 0;
DECLARE temp_shot_counter INT DEFAULT 1;
DECLARE setup_time INT;
DECLARE cursor1 CURSOR FOR
SELECT
(schedule_run_time*3600)/(schedule_qty/schedule_cavity_count) as shot_ct,
ROUND((schedule_qty/schedule_cavity_count)) as shot_count,
schedule_mold as shot_mold, schedule_cavity_count as shot_cavity_count
FROM `schedule`
WHERE `schedule`.schedule_cell = start_cell AND `schedule`.schedule_date = DATE(start_time) AND deleted = 0
ORDER BY schedule_order ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SELECT COUNT(schedule_id) INTO temp_job_count FROM `schedule` WHERE schedule_cell = start_cell AND schedule_date = DATE(start_time) AND deleted = 0;
SELECT cell_setup_standard INTO setup_time FROM cells WHERE cell_name = start_cell;
DROP TEMPORARY TABLE IF EXISTS rst;
CREATE TEMPORARY TABLE rst (
shot_id INT NOT NULL AUTO_INCREMENT,
shot_datetime datetime,
shot_qty INT,
PRIMARY KEY (shot_id)
);
SET temp_time = start_time;
SET temp_mold = "";
SET temp_row_count = 0;
INSERT INTO rst (shot_datetime) VALUES (temp_time);
#Loop through cursor1 recordset
OPEN cursor1;
start_loop: LOOP
FETCH cursor1 INTO temp_shot_ct, temp_shot_count, temp_shot_mold, temp_shot_cavity_count;
IF done THEN
LEAVE start_loop;
END IF;
IF temp_mold <> temp_shot_mold THEN
SET temp_time = TIMESTAMPADD(MINUTE,setup_time,temp_time); #Add standard setup time whenever the mold changes
INSERT INTO rst (shot_datetime) VALUES (temp_time);
SET temp_mold = temp_shot_mold;
END IF;
SET temp_shot_counter = 1;
REPEAT
SET temp_time = TIMESTAMPADD(SECOND,temp_shot_ct,temp_time);
INSERT INTO rst (shot_datetime,shot_qty) VALUES (temp_time,temp_shot_cavity_count);
SET temp_shot_counter = temp_shot_counter + 1;
UNTIL temp_shot_counter > temp_shot_count
END REPEAT;
END LOOP;
CLOSE cursor1;
#Return records. Will be used in ORM to loop through records on the PHP side
SELECT SUM(shot_qty) AS qty, HOUR(shot_datetime) AS shot_hour FROM rst GROUP BY HOUR(shot_datetime);
DROP TEMPORARY TABLE IF EXISTS rst;
END
I'm not the greatest at using stored procedures. This one is running very slow - it takes about 6 seconds to run. The queries individually run in milliseconds, so I was expecting this to take 100 milliseconds at most. I can't figure out what I'm doing wrong that's causing this to run so slowly. Does anyone have any ideas on how to make this run quicker? I can create a PHP script to do the same thing, but I'm trying to get better at using stored procs and functions in MySQL - and this is far less code. I welcome any and all advice.

how to update multiple columns from a trigger MySQL

hi i am using mysql trigger to update a table on another table's insertion
this trigger works fine
CREATE TRIGGER `update_pupil_subject` AFTER INSERT ON `pupil_marks`
FOR EACH ROW
BEGIN
UPDATE pupil_subjects SET NumberOfStudens = NumberOfStudens + 1 WHERE NEW.SubjectID = SubjectID;
END$$
but this gives an error
CREATE TRIGGER `update_pupil_subject` AFTER INSERT ON `pupil_marks`
FOR EACH ROW
BEGIN
UPDATE pupil_subjects SET NumberOfStudens = NumberOfStudens + 1 , AverageMarks = (SELECT AVG(Marks) FROM pupil_marks WHERE NEW.StudentID = StudentID ) WHERE NEW.SubjectID = SubjectID;
END$$
how to write this correctly , please help . thanks in advance .
Apparently there were problems when sub-queries were used:
Can you try splitting the SQL statement:
DELIMITER $$
CREATE TRIGGER `update_pupil_subject`
AFTER INSERT
ON `pupil_marks`
FOR EACH ROW
BEGIN
DECLARE avg_marks float;
SELECT AVG(Marks)
INTO avg_marks
FROM pupil_marks
WHERE NEW.SubjectID = SubjectID;
UPDATE pupil_subjects
SET NumberOfStudens = NumberOfStudens + 1, AverageMarks = avg_marks
WHERE NEW.SubjectID = SubjectID;
END
$$
Edit: Use
SHOW TRIGGERS WHERE `table` = 'pupil_marks';
to get all triggers defined on pupil_marks. You can't have multiple triggers on an event as all actions can be covered in single trigger.
NOTE: I think AVG(Marks) is for a given subject, so modified trigger definition accordingly.
declare a variable inside the trigger and assign it with the subquery
declare avg_mark integer default 0;
set avg_mark := (SELECT AVG(Marks) FROM pupil_marks WHERE NEW.StudentID = StudentID);
then use the variable "avg_mark" in your update statement...
it may work...
if not then check the delimiter just below phpmyadmin sql box . It should be "$$"

trigger error with null output

here is my trigger:
create trigger wyplacamy before insert on `skladki_wyplaty`
for each row
BEGIN
SELECT p.wysokosc_skladki, p.ilosc_miesiecy INTO #wysokosc_skladki, #ilosc_miesiecy
FROM skladki_aktualne p
WHERE p.pesel = NEW.pesel AND p.id_ubezpieczenia = NEW.id_ubezpieczenia;
SET NEW.wyoskosc_wyplaty = #wysokosc_skladki / #ilosc_miesiecy *75;
END
$$
This works, but the iutpput of "wyoskosc_wyplaty" is NULL, even wysokosc_skladki = 8000, and ilosc_miesiecy = 8.
You need to declare the variables before using them.
delimiter $$
create trigger wyplacamy before insert on `skladki_wyplaty`
for each row
BEGIN
DECLARE var_wysokosc_skladki INT;
DECLARE var_ilosc_miesiecy INT;
SELECT p.wysokosc_skladki, p.ilosc_miesiecy INTO var_wysokosc_skladki, var_ilosc_miesiecy
FROM skladki_aktualne p
WHERE p.pesel = NEW.pesel
AND p.id_ubezpieczenia = NEW.id_ubezpieczenia;
SET NEW.wyoskosc_wyplaty = var_wysokosc_skladki / var_ilosc_miesiecy * 75;
END
$$

Categories