mysql trigger after insert with 3 tables











up vote
0
down vote

favorite












I have 3 tables: the first one gets all records to the t1 table, the second one is where my whitelist entries are, and the third one is for making some decision in the future regarding to t2.
I need to create after insert trigger an update via my third table.
the steps :




  1. some value come into the tables called "t1"

  2. need to check if the value is in "t2"

  3. if the value is in t2 so go to t3 and do something, if the value is not in t2 so go to t3 and do something else.

  4. but the value will always need to enter t1.


This is my code and it does not work, I don't know how to apply it I've tried many options already.



CREATE TRIGGER check_whitelist AFTER INSERT ON t1
FOR EACH ROW
SELECT
IF(IFNULL(new.name=(
select name From t2 where (t2.name = new.name),
UPDATE t3 SET option1 =0 ,
UPDATE t3 SET option2 =1
)))


Please help me with writing this code.










share|improve this question









New contributor




a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I have 3 tables: the first one gets all records to the t1 table, the second one is where my whitelist entries are, and the third one is for making some decision in the future regarding to t2.
    I need to create after insert trigger an update via my third table.
    the steps :




    1. some value come into the tables called "t1"

    2. need to check if the value is in "t2"

    3. if the value is in t2 so go to t3 and do something, if the value is not in t2 so go to t3 and do something else.

    4. but the value will always need to enter t1.


    This is my code and it does not work, I don't know how to apply it I've tried many options already.



    CREATE TRIGGER check_whitelist AFTER INSERT ON t1
    FOR EACH ROW
    SELECT
    IF(IFNULL(new.name=(
    select name From t2 where (t2.name = new.name),
    UPDATE t3 SET option1 =0 ,
    UPDATE t3 SET option2 =1
    )))


    Please help me with writing this code.










    share|improve this question









    New contributor




    a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have 3 tables: the first one gets all records to the t1 table, the second one is where my whitelist entries are, and the third one is for making some decision in the future regarding to t2.
      I need to create after insert trigger an update via my third table.
      the steps :




      1. some value come into the tables called "t1"

      2. need to check if the value is in "t2"

      3. if the value is in t2 so go to t3 and do something, if the value is not in t2 so go to t3 and do something else.

      4. but the value will always need to enter t1.


      This is my code and it does not work, I don't know how to apply it I've tried many options already.



      CREATE TRIGGER check_whitelist AFTER INSERT ON t1
      FOR EACH ROW
      SELECT
      IF(IFNULL(new.name=(
      select name From t2 where (t2.name = new.name),
      UPDATE t3 SET option1 =0 ,
      UPDATE t3 SET option2 =1
      )))


      Please help me with writing this code.










      share|improve this question









      New contributor




      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have 3 tables: the first one gets all records to the t1 table, the second one is where my whitelist entries are, and the third one is for making some decision in the future regarding to t2.
      I need to create after insert trigger an update via my third table.
      the steps :




      1. some value come into the tables called "t1"

      2. need to check if the value is in "t2"

      3. if the value is in t2 so go to t3 and do something, if the value is not in t2 so go to t3 and do something else.

      4. but the value will always need to enter t1.


      This is my code and it does not work, I don't know how to apply it I've tried many options already.



      CREATE TRIGGER check_whitelist AFTER INSERT ON t1
      FOR EACH ROW
      SELECT
      IF(IFNULL(new.name=(
      select name From t2 where (t2.name = new.name),
      UPDATE t3 SET option1 =0 ,
      UPDATE t3 SET option2 =1
      )))


      Please help me with writing this code.







      mysql






      share|improve this question









      New contributor




      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 23 hours ago









      Yoram de Langen

      3,82611524




      3,82611524






      New contributor




      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 23 hours ago









      a.artur

      12




      12




      New contributor




      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      a.artur is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can't put an UPDATE query inside a SELECT query. Use an IF() expression in the value that you're assigning to option1.



          FOR EACH ROW
          UPDATE t3 SET option1 = IF(new.name = (SELECT name FROM t2 WHERE t2.plate = new.name),
          0, 1)





          share|improve this answer





















          • thank you very much its work for me.
            – a.artur
            20 hours ago











          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',
          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
          });


          }
          });






          a.artur is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372160%2fmysql-trigger-after-insert-with-3-tables%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








          up vote
          0
          down vote













          You can't put an UPDATE query inside a SELECT query. Use an IF() expression in the value that you're assigning to option1.



          FOR EACH ROW
          UPDATE t3 SET option1 = IF(new.name = (SELECT name FROM t2 WHERE t2.plate = new.name),
          0, 1)





          share|improve this answer





















          • thank you very much its work for me.
            – a.artur
            20 hours ago















          up vote
          0
          down vote













          You can't put an UPDATE query inside a SELECT query. Use an IF() expression in the value that you're assigning to option1.



          FOR EACH ROW
          UPDATE t3 SET option1 = IF(new.name = (SELECT name FROM t2 WHERE t2.plate = new.name),
          0, 1)





          share|improve this answer





















          • thank you very much its work for me.
            – a.artur
            20 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can't put an UPDATE query inside a SELECT query. Use an IF() expression in the value that you're assigning to option1.



          FOR EACH ROW
          UPDATE t3 SET option1 = IF(new.name = (SELECT name FROM t2 WHERE t2.plate = new.name),
          0, 1)





          share|improve this answer












          You can't put an UPDATE query inside a SELECT query. Use an IF() expression in the value that you're assigning to option1.



          FOR EACH ROW
          UPDATE t3 SET option1 = IF(new.name = (SELECT name FROM t2 WHERE t2.plate = new.name),
          0, 1)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 23 hours ago









          Barmar

          412k34237339




          412k34237339












          • thank you very much its work for me.
            – a.artur
            20 hours ago


















          • thank you very much its work for me.
            – a.artur
            20 hours ago
















          thank you very much its work for me.
          – a.artur
          20 hours ago




          thank you very much its work for me.
          – a.artur
          20 hours ago










          a.artur is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          a.artur is a new contributor. Be nice, and check out our Code of Conduct.













          a.artur is a new contributor. Be nice, and check out our Code of Conduct.












          a.artur is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372160%2fmysql-trigger-after-insert-with-3-tables%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

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]