mysql - Automatically update occurrences in another table

前端 未结 2 544
你的背包
你的背包 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)

提交回复
热议问题