IF condition on MySQL trigger - mysql

I have two tables (Table A, Table B) with the same fields (user_id, domi, ipiresia).
Users can add values in Table A. I want these values to be inserted automatically in Table B BUT only when the user_id is not included in Table B.
on.
CREATE TRIGGER new_value_added
AFTER INSERT ON table_a
FOR EACH ROW
IF NEW.user_id != user_id
INSERT INTO table_b (user_id, domi, ipiresia)
VALUES (NEW.user_id, NEW.domi, NEW.ipiresia);
I know that the IF statement is wrong because the trigger doesn't work as expected.
Any help?

You don't need an IF at all. Just do an insert select on your trigger like this:
INSERT INTO table_b (user_id, domi, ipiresia)
SELECT NEW.user_id, NEW.domi, NEW.ipiresia
FROM table_a
WHERE NOT EXISTS (select 1 from table_b where user_id = NEW.user_id);

Related

MySQL Update Linked Table If Master Table Has New Records

I need to compare two tables that are linked by Foreign Key and add records from the master table that are not already in the linked table with foreign key. What's the best way to go about doing this please?
You can create a trigger on master table that will be triggered when there's a new insert to the master table.
DELIMITER //
CREATE TRIGGER master_after_insert
AFTER INSERT
ON linked_table FOR EACH ROW
BEGIN
-- Insert record into linked_table
INSERT INTO linked_table
( id,
some_value,
master_id)
VALUES
( 1,
'test value',
NEW.id ); // <-- using NEW you can get newly inserted record
END; //
this seemed to do the trick for me:
INSERT into table2 (foreignkey_id)
SELECT id FROM table1
WHERE id NOT IN (SELECT foreignkey_id FROM table2)

Update and add new records table with join in MySQL

I have main table, mytable, with columns id,a,b,c,d
and a Temp_TABLE with columns id,b,c
I want to update mytable with the values on
temp Temp_TABLE, and if the records are not in mytable then insert them.
So far I have the following:
UPDATE mytable
JOIN
Temp_TABLE
ON mytable.profileId = Temp_TABLE.profileId
SET mytable.b = Temp_TABLE.b
mytable.c = Temp_TABLE.c
But this only works for the first part.
How can I insert the the records from Temp_TABLE into mytable
In MySQL, use insert on duplicate key update. But first you have to define the criteria for recognizing duplicates. For that, you need a unique index (or constraint):
create unique index unq_mytable_profileId on mytable(profileid);
Then:
insert into mytable (profileid, b, c)
select tt.profileid, tt.b, tt.c
from temp_table tt
on duplicate key update b = values(b), c = values(c);

Mysql triggers passing data from one table to another matching tables

I've table1 with columns (id, firstname, lastname) and table2 with columns (id, firstname, lastname, ans3, mandate, matchid). So far i've made this trigger that inserts firstname and lastname from table1 to table2. Now the thing that i want is to join the matchid from table2 with the id from table1. For example when matchid = 1 display records from table1 with id = 1 and in the same row the table2 records. Here's what i do so far with the trigger.
DELIMITER //
CREATE TRIGGER after_user_insert AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2 (firstname, lastname, matchid)
VALUES (NEW.firstname, NEW.lastname, NEW.id);
END; //
DELIMITER ;
Here's a photo of what i get in phpmyadmin. You can see that i didn't figure how to join table2.matchid to table1.id
---edit---
From your comments it sounds like perhaps you intended to update a user record which already exists in table2. If so, you can use ON DUPLICATE KEY when you update:
DELIMITER //
CREATE TRIGGER after_user_insert AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2 (firstname, lastname, matchid)
VALUES (NEW.firstname, NEW.lastname, NEW.id)
ON DUPLICATE KEY UPDATE matchid = NEW.id;
END; //
DELIMITER ;
This solution would require that there is a unique index on both the first and last name in table2. If you don't have such a unique index, then you can add one:
ALTER TABLE table2 ADD UNIQUE unq_idx (firstname, lastname);

MySQL if condition inside a trigger

I have 2 tables, lets name it table A and table B, every time something is inserted in the table A I want some of the data (id, name, comment) automatically inserts also in the table B but only if there is no raw in table B with the same name.
Example:
If I insert OBJECT1 in A with values:
id=1, colour=2, name=example, price=5€, comment="blablabla"
it should be inserted in the table B a row with this values:
id=1, name=example, comment="blablabla"
but if then I insert OBJECT2 in A with values:
id=2, colour=5, name=example, price=10€, comment="aaa";
nothing should happen in the table B, because there is already a row in table B with name=example.
This is my code:
CREATE TRIGGER mytrigger
BEFORE INSERT ON tablea
IF NOT EXISTS (SELECT * FROM tableb WHERE tableb.name = NEW.name)
THEN
INSERT INTO tableb (id, name, comment)
VALUES (NEW.id, NEW.name, NEW.comment)
END IF
And this is the error I receive:
You have an error in your SQL Syntax...
I also tried adding BEGIN and END at the starting and end of the definition, also tried with END instead of END IF, also tried BEGIN instead of THEN
Does someone know what am I doing wrong?
In PL/SQL, you can't put an EXISTS() in an IF statement.
So please try this:
CREATE OR REPLACE TRIGGER mytrigger
BEFORE INSERT ON tablea REFERENCING NEW AS NEW
FOR EACH ROW
declare cnt integer;
BEGIN
SELECT count(*) into cnt FROM tableb WHERE tableb.name = :NEW.name;
IF cnt >0
THEN
INSERT INTO tableb (id, name, comment)
VALUES (:NEW.id, :NEW.name, :NEW.comment)
END IF
END;

BEFORE INSERT TRIGGER on one table but insert should be done on another table

I have two tables say table1 and table2.
fields of table1 - id and count
fields of table2 - id and count
both tables have same fields. Now I want to create BEFORE INSERT TRIGGER on table1 which will check if count = 2 then insert into table2 otherwise in table1.
I created the trigger but when I fire this query ..example-
insert into table1 (id, count) values (1323, 2);
then one row is inserted into table2 (this I want) and same row is inserted into table1 also.
I want row should be inserted in table1 or table not in both tables.
what I do to achieve this ?
Try with this:
delimiter $$
CREATE TRIGGER myTrigger
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
IF NEW.count = 2 THEN
insert into table2 (id, count) values (New.id,New.Count);
ELSE
<<no action>>
END IF;
END$$
I'm not sure why you want this, so I suspect I might be giving advice on some sort of solution that should be fixed differently, but here we go.
As you can see in this question here it is not possible to neatly stop an insert. You could try and make an error so you'll not actually insert, but I think that is really painful.
What you can do is make a fake table dummytable and put your insert trigger there. Your row will always insert in the dummy table, but you either insert it in table1 or table2 according to your wishes. The meat of the trigger would look like
IF NEW.count = 2 THEN
INSERT INTO table2 (id, count) VALUES (New.id,New.Count);
ELSE
INSERT INTO table2 (id, count) VALUES (New.id,New.Count);
END IF;
You'll have to clean the dummytable now and then.