Update multiple tables with single query, if a condition is met? - mysql

How do I update 2 tables in single UPDATE SET, under some condition ?
If the condition is not met, I'd only update one table..
I'd like to do something like this:
UPDATE tab1, tab2 SET
tab1.value2=7,
CASE tab1.value1 IS NOT NULL
WHEN true
THEN tab2.value1 = tab1.value1
END
WHERE tab1.id=1 AND tab2.id = tab1.tab2_fk_id
MySQL workbench complains: Syntax Error: Unexpected CASE_SYM
I think I should do this with a TRIGGER - function

Here how you can do it,
update tab1 ,tab2
join tab2 on tab2.id = tab1.tab2_fk_id
set
tab1.value2=7,
tab2.value1 = case when tab1.value1 IS NOT NULL then tab1.value1 else tab2.value1 end
where tab1.id=1

Many thanx to Abhik Chakraborty for a brillant solution. It will be surely useful in other context. I have though decided to create a trigger in static_conveyor (~ tab1 in the above example) to achieve equal behavior. I use real tables and column names here instead of generic.. :
USE `tca`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `static_conveyor_AUPD` AFTER UPDATE ON `static_conveyor` FOR EACH ROW
BEGIN
IF (OLD.signal != NEW.signal)
THEN
IF ((NEW.pallet_id IS NOT NULL) AND (NEW.default_empty IS NOT NULL))
THEN
UPDATE pallet SET empty = NEW.default_empty WHERE pallet.id = NEW.pallet_id;
END IF;
END IF;
END
Works nice ..

Related

Trying to resolve a syntax error for MYSQL update trigger

I am trying to create a trigger to update the Project table whenever the Assign table is updated, yet I keep receiving this error: 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 'END IF
Does anyone have any idea of why I am getting this syntax error? Please note that I am a beginner at this so my SQL statements may be entirely wrong. Also, I know for sure that my query being ran in my IF statement works because I've ran it on its own without being inside a trigger.
delimiter $$
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
#RUN THIS QUERY TO UPDATE MY TABLE
UPDATE Project p JOIN
(SELECT Project.projNo, COUNT(DISTINCT empid) as numEmployeeAssigned
FROM (Project LEFT JOIN Assign ON Project.projNo=Assign.projNo)
GROUP BY projNo
) as tb
ON p.projNo = tb.projNo
SET p.numEmployeeAssigned = tb.numEmployeeAssigned
#END QUERY
END IF
END$$
delimiter;
You can just increment or decrement the count:
UPDATE Project p
SET p.numEmployeeAssigned = p.numEmployeeAssigned +
(case when p.projNo = new.ProjNo then 1 else -1 end)
WHERE p.projNo IN (old.ProjNo, new.ProjNo) AND
old.ProjNo <> new.ProjNo;
Obviously, your code is missing semi-colons after the UPDATE statement and after the END IF.
However, let me suggest that the UPDATE statement could most likely be simplified. As I understand your code, you want to update the count of employees in Project whenever an employee changes its assignment. If so, there is no need to actually query assign. You can just use conditional logic like this:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
IF OLD.projNo <> new.projNo THEN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo);
END IF;
END$$
You could even remove the IF statement and put that condition in the query directly:
CREATE TRIGGER update_trigger AFTER UPDATE ON Assign
FOR EACH ROW
BEGIN
UPDATE Project p
SET numEmployeeAssigned = CASE
WHEN projNo = new.projNo THEN numEmployeeAssigned + 1
ELSE numEmployeeAssigned - 1
END
WHERE projNo IN (OLD.projNo, NEW.projNo) AND OLD.projNo <> NEW.projNo
END$$

Mysql issue update command in before update trigger

Looking for assistance crafting a mysql trigger. My current code does not work as intended. What I would like to do is if field A in table A is modified, copy field A to field B in table A.
Current code looks like this:
BEGIN
IF new.set_id=301 THEN
UPDATE lighting_io_settings SET slider1_val=new.val WHERE set_id=402;
END IF;
END
Obviously it fails because an update is calling an update.
On before update you can check the old tuple value on variable "OLD" and check the new tuple values on "NEW" variable.
BEGIN
IF NEW.columnA != OLD.columnA THEN
//do whatever you want here like
NEW.columnB = NEW.columnA;
//can call update again, just don't change the columnA
update tableA set columnB = NEW.columnA where id = 402;
END IF;
END

What is wrong with the following if statement inside MySql trigger

I'm building a trigger that updates a table when another table get updated. But, for some reason MySql doesn't like the if statement inside it.
The scenario is, there is a table named Group that has level label configuration that are stored in level1, level2, and level3 columns. The other table, Membership, stores member configuration with their level title.
For example if there is a member has level1 title, novice, and there's an update in Group_configuration level1 from 'novice' into 'newbie', trigger will update the member title into 'newbie'. This also applies to level2 and level3.
So my trigger code looks like this:
CREATE TRIGGER update_member_rank_label AFTER UPDATE ON `group`
FOR EACH ROW
begin
if (NEW.level1 <> OLD.level1 ) then
UPDATE membership set level = NEW.level1 where level=OLD.level1 and gid=old.id;
else if (NEW.level2 <> OLD.level2 ) then
UPDATE membership set level = NEW.level2 where level=OLD.level2 and gid=old.id;
else if (NEW.level3 <> OLD.level3 ) then
UPDATE membership set level = NEW.level3 where level=OLD.level3 and gid=old.id
end if;
end;
but I keep 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 '' at line 5
Any ideas where do I miss?
regards
please check that you set the delimiter to something else than ; before actually creating the trigger:
DELIMITER |
CREATE TRIGGER update_member_rank_label AFTER UPDATE ON `group`
FOR EACH ROW
begin
if (NEW.level1 <> OLD.level1 ) then
UPDATE membership set level = NEW.level1 where level=OLD.level1 and gid=old.id;
else if (NEW.level2 <> OLD.level2 ) then
UPDATE membership set level = NEW.level2 where level=OLD.level2 and gid=old.id;
else if (NEW.level3 <> OLD.level3 ) then
UPDATE membership set level = NEW.level3 where level=OLD.level3 and gid=old.id
end if;
end;
DELIMITER ;
Maybe this helps?
On line 9 add a semicolon. I believe your entire conditional is confused because of this.

MySQL nested Conditional in Update Trigger

I am trying to figure out a conditional trigger for an update to a MySQL table.
The issue is that the update trigger fires off even when the data being entered is the same as the data that was already there. So if I have a column with a row:value set up like this plus_votes:2 if I update with UPDATE votes SET plus_votes = 2; even though nothing has really changed the UPDATE trigger still gets fired off. So to prevent all of this I want to add a conditional clause to my trigger like this
BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
UPDATE my_other_table
SET plus_votes =
CASE NEW.vote_value WHEN '1'
THEN plus_votes + 1
ELSE plus_votes
END,
minus_votes =
CASE NEW.vote_value WHEN '1'
THEN minus_votes
ELSE minus_votes +1
END
WHERE my_other_table.id=NEW.votes_join_id;
END
END
the second half of the issue is that I have CASE condition inside the IF statement.
Can someone help on this issiu and show how to do nested conditionals inside a TRIGGER?
much appreciated
although the syntax you have posted is completely wrong, I have figured out what you want try this ( not tested )
DROP TRIGGER IF EXISTS upd_vote;
DELIMITER $$
CREATE TRIGGER upd_vote AFTER UPDATE ON `my_other_table`
FOR EACH ROW BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
SET
NEW.plus_votes = IF(NEW.vote_value=1,OLD.plus_votes+1,OLD.plus_votes),
NEW.minus_votes = IF(NEW.vote_value=1,OLD.minus_votes,OLD.minus_votes +1 )
END IF;
END$$
DELIMITER ;

Mysql trigger with two tables and if statement

I cannot find any solution for this 'problem'.
My database needs to update a field in one table when another table is updated.
(only when the new value is 5 or 7 and the new prop.key is "status_id")
But the updated table doesn't include the values that I need. It only contains an ID to another table.
I am not sure about the syntax for this statement. This one won't work. Can anybode help me?
This is what I tried:
delimiter $$
CREATE TRIGGER autoClosedOn AFTER INSERT ON journal_details
FOR EACH ROW BEGIN
IF
NEW.prop_key = "status_id"
AND NEW.value = "5"
OR NEW.value = "7"
THEN
UPDATE
issues i
JOIN journals j
ON j.journalized_id = i.id
SET
i.closed_on = j.created_on;
WHERE j.id = NEW.jounal_id;
END IF;
END$$
delimiter ;
You have a superfluous semicolon before the WHERE clause of your update statement.