trigger to edit other table in mysql - mysql

I have the following trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS trg_day_1_status_update$$
USE `tbl_user_status`$$
CREATE DEFINER = CURRENT_USER TRIGGER `tbl_user_status`.`trg_day_1_status_update` AFTER INSERT ON `tbl_tf_day_1` FOR EACH ROW
BEGIN
UPDATE tbl_user_status
SET NEW.roadmap_day = tbl_tf_day_1.roadmap_day,
SET NEW.user_status = "active",
SET NEW.latest_submit = tbl_tf_day_1.submitted_on,
SET NEW.latest_tf_id = tbl_tf_day_1.tf_id,
SET NEW.d0 = 1,
SET NEW.latest_cig_intake = tbl_tf_day_1.q_id_3_ftnd,
SET NEW.latest_cigintake_submit = tbl_tf_day_1.submitted_on
WHERE id_user = tbl_tf_day_1.id_user LIMIT 1;
END;
$$
DELIMITER ;
But I keep getting an error:
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 NEW.user_status = "active",
SET NEW.latest_submit = tbl_tf_day_1.submitted' at line 5
I've tried a bunch of stuff, including removing the 'NEW' before the fields, changing the value to be inserted, and even making this trigger BEFORE the insert. Can't figure out what's wrong, please help!

A few things wrong here, set only needs to be stated once, the direction of the set statements is incorrect , the column to the left of the equals (=) should the the column you wish to set in the table you wish to update and the column to the right of the equals (=) should the the column you wish to update it from pre- ceeded by a NEW. qualifier (but not for constants such as 'active', the where clause column name to the right of the equals (=) should be preceeded by NEW. I am also a bit dubious about the table name you are trying to update which seeems to share a name with the db/schema in your create statement.

You only need to use the SET keyword once, then comma-separate the fields, like this:
UPDATE tbl_user_status SET
NEW.roadmap_day = tbl_tf_day_1.roadmap_day,
NEW.user_status = "active",
NEW.latest_submit = tbl_tf_day_1.submitted_on,
NEW.latest_tf_id = tbl_tf_day_1.tf_id,
NEW.d0 = 1,
NEW.latest_cig_intake = tbl_tf_day_1.q_id_3_ftnd,
NEW.latest_cigintake_submit = tbl_tf_day_1.submitted_on
WHERE id_user = tbl_tf_day_1.id_user LIMIT 1;

Related

MySQL trigger is producing a syntax error, but I'm not sure what is wrong with it

I've created a trigger to update one table once an item is inserted into another. The trigger will also check one of the values being inserted into the new table, and using an if/elseif, will perform the appropriate Update query. I'm running in to a syntax error, but I'm not sure what's wrong with my syntax
CREATE TRIGGER Create_Media_Like_Trigger AFTER INSERT ON MediaLike
FOR EACH ROW
IF NEW.likeType = 'LIKE' THEN
UPDATE Media
SET Media.numLikes = Media.numLikes + 1
WHERE Media.mediaId = NEW.mediaId
ELSEIF NEW.likeType = 'DISLIKE' THEN
UPDATE Media
SET Media.numLikes = Media.numLikes - 1
WHERE Media.mediaId = NEW.mediaId
END IF;
I'm receiving an syntax error on or near the ELSEIF line, but I cant figure out what is wrong with it

MySQL - phpmyadmin - swap columns

I try swap 3 columns in my table. I try this :
DELIMITER $$
CREATE PROCEDURE px()
BEGIN
DECLARE temp VARCHAR(20);
update `idsaccess` set
temp = referer,
referer = size_var,
size_var = agent,
agent = temp
WHERE agent like '%210%' ;
END $$
CALL p
It don't work. It give me that error: Unknown column 'temp' in 'field list' I do not understand that: temp is varchar value not a column. I also try remove DECLARE and PROCEDURE and just set variable with #. Like this:
set #temp = '';
update `idsaccess`
set #temp = referer,
referer = size_var,
size_var = agent
agent = #temp
WHERE agent like '%210%'
It don't work either. It give me. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax. Any idea what is wrong in my code ? And to avoid misunderstanding I don't want move columns. I just want swap SOME rows (WHERE agent like '%210%') from one column to another.
Try this after taking a backup... I tried for table I have with 2 columns and worked
UPDATE idsaccess SET referer=#tmp:=referer, referer=size_var, size_var = agent, agent = #tmp WHERE agent like '%210%';

MYSQL Trigger to update different table based on inserted value

I need to update oc_product_attribute with a value obtained from an inserted row in vehicles. How can I get this specific value? (xxxCOLUMN_VALUExxx)
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute SET oc_product_attribute.vehicle_name =
(SELECT CONCAT(vehicles.make_name, ' ', vehicles.model_name, ' ',
vehicles.chassis) FROM vehicles WHERE vehicles.id = xxxCOLUMN_VALUExxx)
WHERE oc_product_attribute.id = xxxCOLUMN_VALUExxx
END
Even simplified below: I still get an error in MYSQL:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis);
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 7
And Simplified further:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name = 'test' WHERE 1
END;
I still get the same error - Could it be MYSQL doesn't have triggers enabled?
I guess you need to update oc_product_attribute like this:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis)
WHERE oc_product_attribute.id = NEW.id;
END
Although I am no sure if oc_product_attribute.id is holding the vehicles.id. However, the important poart is that in an INSERT AFTER TRIGGER you can access the values of the inserted row by NEW.columnName.
N.B. In update triggers you also can access the value before the change with OLD.columnName

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.

MySQL Trigger not saving

Executing this:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END
Results in this:
/* SQL 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 8 */
What am I missing? This should work, shouldn't it?
Thanks
You're most likely trying to add the trigger without changing the delimiter. Since the trigger contains semicolon, you'll have to change the delimiter to something else temporarily;
DELIMITER //
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END //
DELIMITER ;
An SQLfiddle with the trigger adding successfully. Note that the delimiter is changed in the settings,
Here's another implementation:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;