mysql double trigger two updates together Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Delimiters in MySQLShould I use the datetime or timestamp data type in MySQL?Are database triggers evil?How to get a list of MySQL user accountsInsert into a MySQL table or update if existsReference - What does this error mean in PHP?How to import an SQL file using the command line in MySQL?Mysql trigger replace rowUPDATE mulitiple rows through triggerMysql trigger, select and then updateMySQL UPDATE trigger error - can't update table already used by statement

How to deal with a team lead who never gives me credit?

Is it fair for a professor to grade us on the possession of past papers?

How does debian/ubuntu knows a package has a updated version

Why do we bend a book to keep it straight?

porting install scripts : can rpm replace apt?

Why is my conclusion inconsistent with the van't Hoff equation?

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Using et al. for a last / senior author rather than for a first author

Why aren't air breathing engines used as small first stages

Can a non-EU citizen traveling with me come with me through the EU passport line?

What does an IRS interview request entail when called in to verify expenses for a sole proprietor small business?

How to bypass password on Windows XP account?

How do I stop a creek from eroding my steep embankment?

Why did the Falcon Heavy center core fall off the ASDS OCISLY barge?

How come Sam didn't become Lord of Horn Hill?

String `!23` is replaced with `docker` in command line

How to run gsettings for another user Ubuntu 18.04.2 LTS

How to tell that you are a giant?

How do pianists reach extremely loud dynamics?

51k Euros annually for a family of 4 in Berlin: Is it enough?

What is the role of the transistor and diode in a soft start circuit?

How to call a function with default parameter through a pointer to function that is the return of another function?

What is Arya's weapon design?

Using audio cues to encourage good posture



mysql double trigger two updates together



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Delimiters in MySQLShould I use the datetime or timestamp data type in MySQL?Are database triggers evil?How to get a list of MySQL user accountsInsert into a MySQL table or update if existsReference - What does this error mean in PHP?How to import an SQL file using the command line in MySQL?Mysql trigger replace rowUPDATE mulitiple rows through triggerMysql trigger, select and then updateMySQL UPDATE trigger error - can't update table already used by statement



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















After trying to create a new trigger in invoices table to UPDATE `invoices` SET invoices.`owes` = (`owes` - `paid`);



I get an error because I already that another trigger in payments that is updating. (see below)



I'm looking to keep the existing trigger below, but how to modify it to also update owes to (owes - paid) in the invoices table.



CREATE TRIGGER `after_payment_update` AFTER UPDATE 
ON `payments`
FOR EACH ROW UPDATE `invoices`
SET invoices.`paid` = (SELECT SUM(payments .`payment`)
FROM payments WHERE payments.`invoice` = invoices.`invoice`)









share|improve this question




























    1















    After trying to create a new trigger in invoices table to UPDATE `invoices` SET invoices.`owes` = (`owes` - `paid`);



    I get an error because I already that another trigger in payments that is updating. (see below)



    I'm looking to keep the existing trigger below, but how to modify it to also update owes to (owes - paid) in the invoices table.



    CREATE TRIGGER `after_payment_update` AFTER UPDATE 
    ON `payments`
    FOR EACH ROW UPDATE `invoices`
    SET invoices.`paid` = (SELECT SUM(payments .`payment`)
    FROM payments WHERE payments.`invoice` = invoices.`invoice`)









    share|improve this question
























      1












      1








      1








      After trying to create a new trigger in invoices table to UPDATE `invoices` SET invoices.`owes` = (`owes` - `paid`);



      I get an error because I already that another trigger in payments that is updating. (see below)



      I'm looking to keep the existing trigger below, but how to modify it to also update owes to (owes - paid) in the invoices table.



      CREATE TRIGGER `after_payment_update` AFTER UPDATE 
      ON `payments`
      FOR EACH ROW UPDATE `invoices`
      SET invoices.`paid` = (SELECT SUM(payments .`payment`)
      FROM payments WHERE payments.`invoice` = invoices.`invoice`)









      share|improve this question














      After trying to create a new trigger in invoices table to UPDATE `invoices` SET invoices.`owes` = (`owes` - `paid`);



      I get an error because I already that another trigger in payments that is updating. (see below)



      I'm looking to keep the existing trigger below, but how to modify it to also update owes to (owes - paid) in the invoices table.



      CREATE TRIGGER `after_payment_update` AFTER UPDATE 
      ON `payments`
      FOR EACH ROW UPDATE `invoices`
      SET invoices.`paid` = (SELECT SUM(payments .`payment`)
      FROM payments WHERE payments.`invoice` = invoices.`invoice`)






      mysql triggers






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 17:37









      BarclayVisionBarclayVision

      5071632




      5071632






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.



          DELIMITER $$
          CREATE TRIGGER after_update_payments
          AFTER UPDATE ON payments
          FOR EACH ROW BEGIN
          UPDATE invoices
          SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
          NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
          END $$
          DELIMITER ;


          You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:



          DELIMITER $$
          CREATE TRIGGER after_update_payments
          AFTER UPDATE ON payments
          FOR EACH ROW BEGIN
          IF (some condition here) THEN
          UPDATE invoices
          SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
          NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
          END IF;
          END $$
          DELIMITER ;


          As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.



          Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:



          CREATE 
          ALGORITHM = UNDEFINED
          DEFINER = `root`@`localhost`
          SQL SECURITY DEFINER
          VIEW invoice_payments_view AS (

          SELECT
          t1.*,
          SUM(t2.payment) as amount_paid,
          SUM(t2.owes - SUM(t2.payment)) as amount_owed
          FROM invoices t1
          JOIN payments t2 ON (t1.invoice=t2.invoice)
          GROUP BY t1.invoice
          )


          Then to access the amount_paid and amount_owed columns, you would simply query the following:



          SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view 
          WHERE invoice=1;


          Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068289%2fmysql-double-trigger-two-updates-together%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.



            DELIMITER $$
            CREATE TRIGGER after_update_payments
            AFTER UPDATE ON payments
            FOR EACH ROW BEGIN
            UPDATE invoices
            SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
            NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
            END $$
            DELIMITER ;


            You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:



            DELIMITER $$
            CREATE TRIGGER after_update_payments
            AFTER UPDATE ON payments
            FOR EACH ROW BEGIN
            IF (some condition here) THEN
            UPDATE invoices
            SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
            NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
            END IF;
            END $$
            DELIMITER ;


            As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.



            Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:



            CREATE 
            ALGORITHM = UNDEFINED
            DEFINER = `root`@`localhost`
            SQL SECURITY DEFINER
            VIEW invoice_payments_view AS (

            SELECT
            t1.*,
            SUM(t2.payment) as amount_paid,
            SUM(t2.owes - SUM(t2.payment)) as amount_owed
            FROM invoices t1
            JOIN payments t2 ON (t1.invoice=t2.invoice)
            GROUP BY t1.invoice
            )


            Then to access the amount_paid and amount_owed columns, you would simply query the following:



            SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view 
            WHERE invoice=1;


            Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.






            share|improve this answer





























              0














              You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.



              DELIMITER $$
              CREATE TRIGGER after_update_payments
              AFTER UPDATE ON payments
              FOR EACH ROW BEGIN
              UPDATE invoices
              SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
              NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
              END $$
              DELIMITER ;


              You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:



              DELIMITER $$
              CREATE TRIGGER after_update_payments
              AFTER UPDATE ON payments
              FOR EACH ROW BEGIN
              IF (some condition here) THEN
              UPDATE invoices
              SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
              NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
              END IF;
              END $$
              DELIMITER ;


              As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.



              Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:



              CREATE 
              ALGORITHM = UNDEFINED
              DEFINER = `root`@`localhost`
              SQL SECURITY DEFINER
              VIEW invoice_payments_view AS (

              SELECT
              t1.*,
              SUM(t2.payment) as amount_paid,
              SUM(t2.owes - SUM(t2.payment)) as amount_owed
              FROM invoices t1
              JOIN payments t2 ON (t1.invoice=t2.invoice)
              GROUP BY t1.invoice
              )


              Then to access the amount_paid and amount_owed columns, you would simply query the following:



              SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view 
              WHERE invoice=1;


              Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.






              share|improve this answer



























                0












                0








                0







                You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.



                DELIMITER $$
                CREATE TRIGGER after_update_payments
                AFTER UPDATE ON payments
                FOR EACH ROW BEGIN
                UPDATE invoices
                SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
                NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
                END $$
                DELIMITER ;


                You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:



                DELIMITER $$
                CREATE TRIGGER after_update_payments
                AFTER UPDATE ON payments
                FOR EACH ROW BEGIN
                IF (some condition here) THEN
                UPDATE invoices
                SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
                NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
                END IF;
                END $$
                DELIMITER ;


                As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.



                Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:



                CREATE 
                ALGORITHM = UNDEFINED
                DEFINER = `root`@`localhost`
                SQL SECURITY DEFINER
                VIEW invoice_payments_view AS (

                SELECT
                t1.*,
                SUM(t2.payment) as amount_paid,
                SUM(t2.owes - SUM(t2.payment)) as amount_owed
                FROM invoices t1
                JOIN payments t2 ON (t1.invoice=t2.invoice)
                GROUP BY t1.invoice
                )


                Then to access the amount_paid and amount_owed columns, you would simply query the following:



                SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view 
                WHERE invoice=1;


                Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.






                share|improve this answer















                You can't create a second trigger that "triggers" on the same action as another trigger. Instead you would use a DELIMITER $$ statement like below and fill your trigger with all the relevant code you want executed.



                DELIMITER $$
                CREATE TRIGGER after_update_payments
                AFTER UPDATE ON payments
                FOR EACH ROW BEGIN
                UPDATE invoices
                SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
                NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
                END $$
                DELIMITER ;


                You don't actually need a DELIMITER in the trigger above, so I will show you an example where you would need to use it:



                DELIMITER $$
                CREATE TRIGGER after_update_payments
                AFTER UPDATE ON payments
                FOR EACH ROW BEGIN
                IF (some condition here) THEN
                UPDATE invoices
                SET NEW.paid = (SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice),
                NEW.owes = (owes -(SELECT SUM(payment) FROM payments WHERE invoice = NEW.invoice));
                END IF;
                END $$
                DELIMITER ;


                As a general rule, if you need to execute multiple statements that need a ; at the end of them, you need to use a DELIMITER. If this still doesn't make sense, a great explanation for delimiters can be found here.



                Now, on a side-note, I don't think this approach is the most optimal one. What I would do in your situation is create a view that combines these tables. For example:



                CREATE 
                ALGORITHM = UNDEFINED
                DEFINER = `root`@`localhost`
                SQL SECURITY DEFINER
                VIEW invoice_payments_view AS (

                SELECT
                t1.*,
                SUM(t2.payment) as amount_paid,
                SUM(t2.owes - SUM(t2.payment)) as amount_owed
                FROM invoices t1
                JOIN payments t2 ON (t1.invoice=t2.invoice)
                GROUP BY t1.invoice
                )


                Then to access the amount_paid and amount_owed columns, you would simply query the following:



                SELECT invoice, amount_paid, amount_owed FROM invoice_payments_view 
                WHERE invoice=1;


                Note that I am by no means an expert on this topic, and I am only showing you how I would approach this situation. Also, I didn't test any of this code, so you might need to modify it slightly. If you have any issues let me know and I can update.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 9 at 1:29

























                answered Mar 9 at 1:18









                Caleb GoodmanCaleb Goodman

                1,2161518




                1,2161518





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068289%2fmysql-double-trigger-two-updates-together%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко