mysql - Automatically update occurrences in another table

前端 未结 2 545
你的背包
你的背包 2021-01-17 05:16

I have two tables, one for storing reports of a user, and another for storing users.

1. Report_table
-----report_id(P)-----user_id--------
        001               


        
相关标签:
2条回答
  • 2021-01-17 05:41

    You need a trigger: http://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html which should fire after each insert operation on the reports_table.

    CREATE TRIGGER update_report_cnt AFTER INSERT ON reports_table
    FOR EACH ROW
       BEGIN
          UPDATE users_table SET no_of_reports = no_of_reports + 1 where user_id = NEW.user_id;
       END;
    

    (I'm not sure the syntax is correct. Ithink a before insert trigger would also work)

    0 讨论(0)
  • 2021-01-17 05:47

    You can deal with TRIGGER by using a AFTER INSERT trigger

    http://dev.mysql.com/doc/refman/5.0/en/triggers.html

    0 讨论(0)
提交回复
热议问题