using a join inside a trigger, MySQL - mysql

I'm developing a database that needs to update a field in one table when another table is updated. However, the table that is being updated doesn't contain the values I need... it just contains an ID to a third table that has that value. Basically, I'm looking for something that does this:
CREATE TRIGGER au_TableA AFTER UPDATE TableA
FOR EACH ROW BEGIN
UPDATE TableB SET points=TableC.points
WHERE TableC.cID=NEW.cID;
END
cID, of course, being present in both TableA and TableC.

Try this query -
UPDATE
TableB b
JOIN TableC с
ON c.cID = b.cID
SET
b.points = c.points
WHERE c.cID=NEW.cID;

Related

Trying to use value from updated row in a where statement in my trigger

I want to use a trigger to automatically update another table but I'm having some problems with it.
DELIMITER $$
DROP TRIGGER IF EXISTS `trigger1` $$
CREATE TRIGGER `trigger1`
AFTER UPDATE ON `table1` FOR EACH ROW
UPDATE `table4`
inner join (SELECT o.`Name`,
o.Date,
(o.`Availability` * (c.Rate)) total
FROM `table2` o
LEFT JOIN `table1` r
ON o.`Name` = r.`Name`
AND o.Date = r.Date
LEFT JOIN (SELECT table3.`Name`,Choke, Rate FROM table3
left join `table1` as w
on table3.`Name` = w.`Name`
and table3.Choke = w.`Size`
where w.`Name` = table3.`Name`
and table3.Date <= w.Date
ORDER BY table3.Date DESC
LIMIT 1) c
ON c.`Name` = o.`Name`)x
set `Contribution` = x.total
where (`table4`.Date) = x.Date and `table4`.`Name` = x.`Name`;
END $$
DELIMITER ;
I would like to use the date from table1 row (that is the table which triggers the trigger) in my left join named c. As it stands c.Rate gives the same value every time because it uses the default table1.
If the row being updated has a date of '2022-01-13' then I want the date used at the line asterisked
and table3.Date <= w.Date
I want w.Date to be '2022-01-13'. But as it stands I can't get that and all the c.Rate give the same value.
Thanks.
The lack of consistent indentation and capitalisation makes your query almost impossible to read. Instead of obfuscating what is going on by using table1, table2, table3 & table4 you would be better off using the real table names, as it will make more sense to anyone trying to read it.
Your current update query makes little sense with the repeated left joins back to the originating table1 but it is hard to be sure given the lack of supporting information in your question. Your first left join to table1, aliased as r, does not get used anywhere. Your second left join to table1, aliased as w, is then referenced in the where clause which turns it into an inner join.
I suggest you update your question with the CREATE TABLE statements and some sample data to show the values before and after executing your update and the trigger update. Your current update query is definitely not the most efficient way of achieving your goal.
I don't really understand your question but it seems that you all are asking is how to use the value from the table1 row being updated? In which case the answer is simply -
and w.Date = NEW.Date
where NEW references the post-update version of the table1 row.
From 25.3.1 Trigger Syntax and Examples -
Within the trigger body, the OLD and NEW keywords enable you to access
columns in the rows affected by a trigger. OLD and NEW are MySQL
extensions to triggers; they are not case-sensitive.
In an INSERT trigger, only NEW.col_name can be used; there is no old
row. In a DELETE trigger, only OLD.col_name can be used; there is no
new row. In an UPDATE trigger, you can use OLD.col_name to refer to
the columns of a row before it is updated and NEW.col_name to refer to
the columns of the row after it is updated.

Insert a new record in MySQL table at start

I am new to MySQL and learning it to my own. Actually I want to copy a column from a table into my existing table column! suppose that my existing table is:
where pid values are inserted by default!
now i want to copy a column from another table using:
INSERT INTO exist_tab(FirstLevel) SELECT some_col FROM another_table;
so that the values should come inside FirstLevel Column.
but the problem is that the copies values come below the pid values in FirstLevel Column as:
see that the firstlevel comes below! what is wrong with it? I need the "H" value against 19 but i dont want to use wild cards just want to copy the new data against old column data
thanks
I am new to this kind a work please can somebody give me any idea how to do it please!
thanks in advance
INSERT and UPDATE is different Command to Perform Different Task.
INSERT :Insert New Record into the table
Update:Update Existing Record in table If Exist.
NOT SURE ABOUT IT:(i'm Not Familiar With MYSQL)
Update a set
a.FirstLevel=b.some_col
from
exist_tab a join another_table b on a.Id=b.Id
Or You can Try :
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table where Id=a.Id)
EDIT2:
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table)
You Can Find Here.
You are using INSERT statement here. INSERT will create a new record in the table. You have to use UPDATE for updating a particular column in the existing table like this:
UPDATE exist_tab
SET FirstLevel = (SELECT some_col FROM another_table)
If you want any conditional update then you can use JOIN like this:
UPDATE exist_tab a
LEFT JOIN another_table b ON
a.pid = b.id
SET FirstLevel = a.some_col;

Getting information from a third table in a MySQL trigger

What I want to do is similar to this, except the action is an insert not an update. I will use the same example used in that question, modified for my context;
I want to do something like this:
CREATE TRIGGER au_TableA AFTER UPDATE TableA
FOR EACH ROW BEGIN
INSERT INTO TableB (cID, points) VALUES (NEW.cID, TableC.points);
END
cID is present in TableA and TableC. Because it's not an UPDATE I can't do a JOIN like in the other question (right?). Is there any way to get the data from TableC INSERTED into TableB here?
If I understand your question correctly, this should be the trigger that you need:
CREATE TRIGGER au_TableA AFTER UPDATE TableA
FOR EACH ROW
BEGIN
INSERT INTO TableB (cID, points)
SELECT TableC.cID, TableC.points
FROM TableC
WHERE TableC.cID = NEW.cID;
END
You don't need a JOIN in this case, but you could use the syntax INSERT INTO ... SELECT

Create trigger to affect specific rows

I want it to do is upon any insert of new rows or edit of existing rows on B, specific the 'last_seen' column would be updated on A.
This is what I have so far:
CREATE TRIGGER insert_records
AFTER UPDATE ON loan_records
FOR EACH ROW
Update temp_card A
INNER JOIN loan_records B
SET A.last_seen = CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no
and this
CREATE TRIGGER insert_records2
AFTER INSERT ON loan_records
FOR EACH ROW
Update temp_card A
INNER JOIN loan_records B
SET A.last_seen =CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no
currently this updates all 'last_seen' columns in A, regardless whether or not they've been updated.
You don't need to JOIN both tables. Just use NEW.card_no in WHERE clause of your UPDATE statement.
Here is how both (AFTER INSERT and AFTER UPDATE) your triggers might look like
CREATE TRIGGER tg_loan_records_insert
AFTER INSERT ON loan_records
FOR EACH ROW
UPDATE temp_card
SET last_seen = NOW()
WHERE card_no = NEW.card_no;
CREATE TRIGGER tg_loan_records_update
AFTER UPDATE ON loan_records
FOR EACH ROW
UPDATE temp_card
SET last_seen = NOW()
WHERE card_no = NEW.card_no;
Here is SQLFiddle demo
you should refer to only currently updated id.
CREATE TRIGGER insert_records2
AFTER INSERT ON loan_records
FOR EACH ROW
Update temp_card A
INNER JOIN loan_records B
SET A.last_seen =CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no and a.id = OLD.id
For more details on how to use for before update, after update please refer
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

Insert column values into other table with JOIN/WHERE clause

I have a simple amateur question.
Table A(login_count) contains all existing userids and their login-count.
Table B(login4buy) contains specific userids and other information.
I want SQL to add the specific login-count from Table A to the specific userid in table B.
This is my try:
INSERT INTO orders_subset
SELECT login_count
FROM login4buy
WHERE login4buy.userid=orders_subset.userid
How can I put the count from Table A into Table B?
I think you want an UPDATE instead of an INSERT
UPDATE lb
SET lb.orders_subset = lc.login_count
FROM login4buy lb
INNER JOIN login_count lc
ON lb.userid = lc.userid
I think you need UPDATE, not INSERT:
UPDATE
orders_subset
JOIN
login4buy
ON login4buy.userid = orders_subset.userid
SET
login4buy.login_count = orders_subset.login_count ;