Help on removing the aliases from a MySQL trigger - mysql

my question is too easy (I guess), below is an example of trigger that I am trying to remove the aliases. I don't know what I am doing wrong I just can't get it right.
DELIMITER #
CREATE TRIGGER StartOfShift BEFORE INSERT ON shift
FOR EACH ROW
BEGIN
IF(NEW.CashierCode NOT IN ( SELECT w.EmployeeID FROM WorksOn as w
JOIN shop AS s ON w.ShopID = s.ShopID
JOIN CashMachineCode AS c ON s.ShopID = c.ShopID
WHERE c.CashMachineCode = NEW.CashMachineCode ))
THEN SET NEW.CashierCode = NULL;
END IF;
END;

The following should be what you are looking for:
DELIMITER #
CREATE TRIGGER StartOfShift BEFORE INSERT ON shift
FOR EACH ROW
BEGIN
IF(NEW.CashierCode NOT IN (
SELECT WorksOn.EmployeeID FROM WorksOn
JOIN shop ON WorksOn.ShopID = shop.ShopID
JOIN CashMachineCode ON shop.ShopID = CashMachineCode.ShopID
WHERE CashMachineCode.CashMachineCode = NEW.CashMachineCode )
) THEN
SET NEW.CashierCode = NULL;
END IF;
END#
DELIMITER ;

Related

Trigger update multiple field in MySql/MariaDB

I need to create a single trigger that runs after inserting a row in the table Documenti and makes the 2 updates shown in the following query
UPDATE AllegatoZip AS a
LEFT JOIN Documenti AS d on d.allegato = a.idAllegato
SET a.data_invio_operatore = d.data_invio_doc
WHERE NEW.allegato = a.idAllegato AND NEW.data_invio_doc > a.data_invio_operatore;
UPDATE AllegatoZip AS a SET a.dataInserimento = now()
WHERE NEW.allegato = a.idAllegato;
The trigger I created with just one update works
CREATE TRIGGER AggiornaDataDocumenti AFTER INSERT ON Documenti
FOR EACH ROW UPDATE AllegatoZip a
LEFT JOIN Documenti d on a.idAllegato = d.allegato
SET a.data_invio_operatore = d.data_invio_doc
WHERE NEW.allegato = a.idAllegato AND NEW.data_invio_doc > a.data_invio_operatore
SQL Database model: https://ibb.co/msnvNe
PS. sorry for my English
UPDATE: I SOLVED
DELIMITER $$
CREATE TRIGGER `AggiornaDataDocumenti` AFTER INSERT ON `Documenti` FOR EACH ROW BEGIN
DECLARE docID INT;
SET docID = NEW.allegato;
UPDATE AllegatoZip a
LEFT JOIN Documenti d on a.idAllegato = d.allegato
SET a.data_invio_operatore = d.data_invio_doc
WHERE
docID = a.idAllegato AND d.data_invio_doc > a.data_invio_operatore;
UPDATE AllegatoZip a SET a.dataInserimento = now()
WHERE
docID = a.idAllegato;
END
$$
DELIMITER ;

MySQL if not exists by two columns update other

I want to insert record if it not exists by 2 columns, and if exists update column 3, here's the query:
INSERT INTO aktai_islaidos_id (akto_id, islaidos_id, savikaina)
SELECT * FROM (SELECT ? as a, ? as b, ? as c) AS tmp
WHERE NOT EXISTS (
SELECT akto_id, islaidos_id, savikaina FROM aktai_islaidos_id
WHERE akto_id = a AND islaidos_id = b
) ON DUPLICATE KEY UPDATE savikaina = VALUES(c);
Now I'm getting error what c not exists in fields list, I understand why, but I didn't know how to complete this query correctly and didn't find any examples like what only where selects duplicate all columns, thanks!
EDIT:
Figured out this with stored procedure:
CREATE PROCEDURE update(
IN akt_id INT,
IN isl_id INT,
IN sav INT)
BEGIN
IF (SELECT count(*) FROM aktai_islaidos_id WHERE akto_id = akt_id AND islaidos_id = isl_id) > 0 THEN
BEGIN
UPDATE aktai_islaidos_id SET savikaina = sav WHERE akto_id = akt_id AND islaidos_id = isl_id;
END;
ELSE
BEGIN
INSERT INTO aktai_islaidos_id (akto_id, islaidos_id, savikaina) VALUES (akt_id, isl_id, sav);
END;
END IF;
END

Update a second table, using trigger

I'm trying to create a simple trigger, but, i can't set the media value on prato if the value comes from #total or #sum.
I've already tested to substitute them for "1" or "1+1", to see if i wasn't even updating correctly or the problem was the operation itself.
DELIMITER $$
CREATE
TRIGGER ratings_prato BEFORE INSERT ON ratings
FOR EACH ROW BEGIN
SET #total = #total + 1;
SET #sum = NEW.stars + #sum;
UPDATE prato p SET p.media = #sum/#total WHERE p.id = 1;
END;
$$
DELIMITER ;
Any ideias?
I've completed mislead the FOR EACH ROW on trigger. I thought, every time it was updated, It would run trough all rows. But, instead of it, it executes for each row it was updated, which, make things possible.
This was the last result of my trigger, inserting on ratings, and updating on food(which was prato)
DELIMITER $$
CREATE
TRIGGER inserting_dishes_rating AFTER INSERT ON ratings
FOR EACH ROW BEGIN
IF(NEW.food_id is not null) THEN
SET #media = (SELECT SUM(stars) FROM ratings WHERE food_id = NEW.food_id) / (SELECT COUNT(*) FROM ratings WHERE food_id = NEW.food_id);
      UPDATE foods f SET f.rate = #media WHERE f.id = NEW.food_id;
    ELSE
  SET #media = (SELECT SUM(stars) FROM ratings WHERE restaurant_id = NEW.restaurant_id) / (SELECT COUNT(*) FROM ratings WHERE restaurant_id = NEW.restaurant_id);  
UPDATE restaurants r SET r.rate = #media WHERE r.id = NEW.restaurant_id;
END IF;
END

MySql Error 1054 when inserting into table that has trigger

I have created a trigger on a table, and when I try to insert data into it I get MySql error "Error Code: 1054 'unknown column license_key in field list'"
The INSERT I try to do is this:
INSERT INTO LOAD_MASTER(license_key, state, total_pageviews, total_visitors, max_visitors, max_pageviews, ip, secret, read_time, url)
VALUES ("order55555hytgtrfderfredfredfredftyu8ikloi98nhygt6", "preparing", 400, 1000, 200, 400, "202,2,35,36", "Hemmeligheden", 120, "http://google.dk");
The Trigger I have created is supposed to check the TABLE LOAD_STATS for the presence of the IP entered in the LOAD_MASTER table, and then based on the results change a value in another table called LICENSE
My trigger is here:
DELIMITER //
CREATE TRIGGER ip_check AFTER INSERT ON LOAD_MASTER FOR EACH ROW
BEGIN
DECLARE MK varchar(50);
DECLARE MIP varchar(15);
DECLARE LID int(6);
SET MK = license_key;
SET MIP = ip;
SET LID = (
SELECT l.license_id
FROM license_keys k, license l
WHERE l.license_id = k.license_id
AND k.license_key = MK
);
IF (SELECT COUNT(DISTICT(master_ip)) FROM LOAD_STATS WHERE master_ip = MIP AND master_key = MK ) > 3 THEN
UPDATE LICENSE
SET STATE = 0
WHERE license_id = LID;
END IF;
END
//
DELIMITER ;
Any help on why I get this error would be much appriciated.
-Dan
In these lines
SET MK = license_key;
SET MIP = ip;
you probably meant to address NEW.license_key and NEW.ip respectively.
IMHO you can make more succinct. Try this
DELIMITER //
CREATE TRIGGER ip_check
AFTER INSERT ON load_master
FOR EACH ROW
BEGIN
IF (SELECT COUNT(DISTICT(master_ip))
FROM load_stats
WHERE master_ip = NEW.ip
AND master_key = NEW.license_key ) > 3 THEN
UPDATE license
SET state = 0
WHERE license_id =
(
SELECT l.license_id
FROM license_keys k JOIN license l
ON l.license_id = k.license_id
AND k.license_key = NEW.license_key
); -- make sure that this subquery returns only ONE record
END IF;
END //
DELIMITER ;
There is some room for improvement rewriting an UPDATE with JOIN instead of using a subquery

How can I get this stored procedure to execute a CALL when the condition is not equal?

I have a quick question about MySql. I have this simple store procedure, that I would like to make a CALL to two store procedures when the condition is met.
The variable #SDOuser is assigned the User ID when the access level is 10 and the timestamp evaluates to the login time of between now and 2 seconds ago. Once I have this set, I then want to use it as the condition for my IF block. Here lies the issue, it works fine when the first block stays TRUE and nothing follows. But, I need the Users who have access level that is less than 10 and it does not seem to like the negating the condition to get to that.
Here is the code:
DROP PROCEDURE `getPositionGrid`//
CREATE DEFINER=<some name> PROCEDURE `getPositionGrid`()
BEGIN
set #SDOuser = (SELECT DISTINCT(uu.uu_id) FROM tbl_logins ul
INNER JOIN tbl_users uu
ON ul.ul_username = uu.uu_email
inner join tbl_access_level al
on al.al_id = uu.al_id
inner join tbl_positions up
on up.up_id = uu.up_id
inner join tbl_departments ud
on up.ud_id = ud.ud_id
WHERE ul.ul_created BETWEEN NOW() - INTERVAL 2 SECOND AND NOW());
set #CurrUser = (select uu_id from tbl_users where al_id = 10 AND uu_id = #SDOuser);
set #CurrUser1 = (select uu_id from tbl_users where al_id < 10 AND uu_id != #SDOuser);
IF(#CurrUser) THEN
BEGIN
CALL getPositionGridSDO();
END;
END IF;
IF(#CurrUser1) THEN
BEGIN
CALL getPositionGridALL();
END;
END IF;
END
//
Try this:
IF (#CurrUser IS NOT NULL) THEN
BEGIN
CALL getPositionGridSDO();
END;
END IF;
IF(#CurrUser1 IS NOT NULL) THEN
BEGIN
CALL getPositionGridALL();
END;
END IF;
Alternatively:
IF (#CurrUser > 0) THEN
...
END IF;
IF(#CurrUser1 > 0) THEN
...
END IF;
Hope it helps!