Mysql triggers passing data from one table to another matching tables - mysql

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);

Related

Pass data from one table to another in mysql

I'm learning mysql and i try to pass data from one table to another.
In Details, i have table1 with columns id, firstname, lastname etc.
Then i have a table2 with columns id, firstname, lastname, bodyfat, bodyweight, matchid.
The thing that i want is to pass the data firstname and lastname from table1 to table2 and they should be matched on table1.id = table2.matchid. Also the procedure should happen every time that new records are inserted into the table1, so the specific values are also updated and displayed in table2.
Is there any way to achieve that?
Thanks, in advance!
Use an after insert 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 ;
This assumes that you only want to insert new records in table2 with the first and last name, which is all the available information in table1. You could include other columns in the insert, but you would have to specify how to do that.
Beyond this, if you needed to initially populate table2 from table1, you could do an INSERT INTO ... SELECT:
INSERT INTO table2 (firstname, lastname)
SELECT firstname, lastname
FROM table1
-- WHERE <conditions>
you can use a trigger on table 1. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.
You can set a trigger on insert event in table 1. Here is a link https://dev.mysql.com/doc/refman/5.7/en/triggers.html that shows examples of trigger in mysql. Hope this helps
You can Create Trigger on Table1 with the insert or update statement into the Table2 using AFTER keyword, so that whenever a data is inserted in the Table 1 the trigger will be invoked and does the respective action based on your trigger definition.
For more details about how to create and use a trigger for MySQL refer the below link.
https://dev.mysql.com/doc/refman/5.7/en/triggers.html

Update column IF exists else insert

I know about ON DUPLICATE USE KEY clauses. But I can not use it since I want to update and insert on non-unique column.
I have table1 and table2. I want to create a trigger on table1.
Pseudocode:
IF id_code for corresponding id_name from table1 is present in table2
then update record in table 2
else record in table2.
For Ex.
table1 has column1 id_code, column2 id_name
table2 has column1 id_code, column2 status
IF id_code for corresponding id_name from table1 is present in table2
UPDATE status column in table2.
ELSE insert id-code in table2
Best way would probably be to use a conditional statement, since as you said you are checking for a non unique value and thus cannot use ON DUPLICATE KEY:
IF EXISTS (SELECT id_code FROM table2 WHERE id_code = 'code from table1') THEN
UPDATE table2 SET status = 'new status' WHERE id_code = 'code from table1';
ELSE
INSERT INTO table2 (id_code, status) VALUES ('code from table1', 'new status');
END IF;
The only caveat here is that control structures are only valid in stored procedures, so you will need to put this in a stored procedure or a trigger.

IF condition on MySQL trigger

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);

Mysql query INSERT two tables

I have two tables:table1 and rating
table1(id,name,category)
rating (cid,rating,total_rating,total_rates,photoID)
and now when i insert data into table1 i want to set all data in table rating at zero for that specific photoID from table1, but i dont know how..can someone help me?
You can use LAST_INSERT_ID() to retrieve the ID you just inserted. For example, assuming PhotoID is the relation between table1 and rating:
insert table1 (name,category) values ('waterfall 2', 'nature');
insert rating (rating,total_rating,total_rates,photoID) values
(0, 0, 0, last_insert_id());
I'd rather create a STORED PROCEDURE to make a single call from application. Assuming that you want to INSERT a record on table rating for every insert on table1 and that ID on table1 is set as AUTO_INCREMENT.
DELIMITER $$
CREATE PROCEDURE procedureName
(
IN _name VARCHAR(25),
IN _category VARCHAR(25)
)
BEGIN
INSERT INTO table1 (name, category)
VALUES (_name, _category);
SET #last_ID := LAST_INSERT_ID();
INSERT INTO rating (cid, rating, total_rating, total_rates, photoID)
VALUES (#last_ID, 0,0,0,0);
END $$
DELIMITER ;
and call the procedure,
CALL procedureName('nameHere','categoryHere')
You can use MySql triggers http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html:
CREATE TRIGGER ins_rating AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO rating (cid,rating,total_rating,total_rates,photoID)
VALUES( NEW.ID ,0,0,0,null)
END;
If you want to insert data into TABLE1 and delete it from TABLE2, you can write below listed query:
mysql_query("DELETE * FROM table2");

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.