简体   繁体   中英

Trigger data in mysql database into another table

I have a table that continuously gets data inserted into table 'rms' from the gateway, I try to use the trigger features in MySQL to apply with the condition to automatically update to another table 'Mergeddata' Table. It appears the problems is that when each time the new data is inserted, the whole data in the database will reinsert into the 'Mergedata' table, which causes duplicating many times in the table. Any ways to fix this?

My code:

Trigger Time: After
Trigger Event :Insert

INSERT INTO processeddata (_NAME,_VALUE,_TIMESTAMP) 
SELECT DISTINCT _NAME, _VALUE, _TIMESTAMP FROM rms WHERE _VALUE = 1

rms table:

id value timestamp
1 0 2022-08-03 10:27:19
2 0 2022-08-03 10:27:24
3 0 2022-08-03 10:27:29
4 1 2022-08-03 10:27:32
5 1 2022-08-03 10:27:32
6 1 2022-08-03 10:27:34

The data needed in table 'Mergeddata'

id value timestamp
1 1 2022-08-03 10:27:32

However, the data repeated each time inserted data The data in 'Mergedata'

id value timestamp
1 1 2022-08-03 10:27:32
2 1 2022-08-03 10:27:32
3 1 2022-08-03 10:27:32
4 1 2022-08-03 10:27:32

Hopefully can get some advice.. enter image description here

If you look at the code generated by the wizard you will see it executes FOR EACH ROW inserted please add the result of SHOW CREATE TRIGGER MergedValue, also in an insert trigger I would expect to see reference to NEW. values (as explained in the manual) making a select from the table which fired the trigger incorrect.

create trigger MergedValue after insert on rms
for each row 
INSERT INTO processeddata (_NAME,_VALUE,_TIMESTAMP) 
 values(new._NAME, new._VALUE, new._TIMESTAMP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM