mysql double trigger two updates together The Next CEO of Stack OverflowDelimiters 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

Cannot shrink btrfs filesystem although there is still data and metadata space left : ERROR: unable to resize '/home': No space left on device

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

Which Pokemon have a special animation when running with them out of their pokeball?

Is it OK to decorate a log book cover?

Is fine stranded wire ok for main supply line?

Is there a reasonable and studied concept of reduction between regular languages?

Is it okay to majorly distort historical facts while writing a fiction story?

Is dried pee considered dirt?

How can the PCs determine if an item is a phylactery?

how one can write a nice vector parser, something that does pgfvecparseA=B-C; D=E x F;

How do you define an element with an ID attribute using LWC?

Are the names of these months realistic?

Airplane gently rocking its wings during whole flight

How to Implement Deterministic Encryption Safely in .NET

Aggressive Under-Indexing and no data for missing index

Can Sneak Attack be used when hitting with an improvised weapon?

"Eavesdropping" vs "Listen in on"

(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?

Is French Guiana a (hard) EU border?

Audio Conversion With ADS1243

Could a dragon use its wings to swim?

Is there an equivalent of cd - for cp or mv

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

Is it convenient to ask the journal's editor for two additional days to complete a review?



mysql double trigger two updates together



The Next CEO of Stack OverflowDelimiters 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










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

      5051632




      5051632






















          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









                JaneJane

                1,1761518




                1,1761518





























                    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

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived