INSERT and UPDATE in one query MYSQL

后端 未结 1 1603
夕颜
夕颜 2021-01-22 14:59

I have a simple INSERT query that is run on event click of a button, this gets variables and inserts them into order table.

I also would like to UPDATE my <

相关标签:
1条回答
  • 2021-01-22 16:01

    If you'll go with a trigger approach then you'll need two triggers (for insert and delete)

    CREATE TRIGGER tg_ai_orders
    AFTER INSERT ON orders
    FOR EACH ROW
      UPDATE bands 
         SET stock = stock - 1
       WHERE band_id = NEW.band_id;
    
    CREATE TRIGGER tg_ad_orders
    AFTER DELETE ON orders
    FOR EACH ROW
      UPDATE bands 
         SET stock = stock + 1
       WHERE band_id = OLD.band_id;
    

    Here is SQLFiddle demo

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