How to link these two tables in sql









up vote
1
down vote

favorite












I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
For example:A user is logged In and want to add others.
If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.



Will become clear from the following scenario.



I'v two table one is user user, and the second is userdata userdata.
Column id is primary key in user and Foreign key in userdata as adder.
I want to insert the data into the table called user when the structure is something like this.



user



 id name

1 Alice
2 Bob
3 jhon


userdata



 adder added
1 2
1 3


the userdata table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata like



 1 2 i.e (**Alice added Bob**)


and when there is second row populated from front end that says



 2 1 i.e (**Bob added Alice**)


so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
    For example:A user is logged In and want to add others.
    If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.



    Will become clear from the following scenario.



    I'v two table one is user user, and the second is userdata userdata.
    Column id is primary key in user and Foreign key in userdata as adder.
    I want to insert the data into the table called user when the structure is something like this.



    user



     id name

    1 Alice
    2 Bob
    3 jhon


    userdata



     adder added
    1 2
    1 3


    the userdata table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata like



     1 2 i.e (**Alice added Bob**)


    and when there is second row populated from front end that says



     2 1 i.e (**Bob added Alice**)


    so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
      For example:A user is logged In and want to add others.
      If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.



      Will become clear from the following scenario.



      I'v two table one is user user, and the second is userdata userdata.
      Column id is primary key in user and Foreign key in userdata as adder.
      I want to insert the data into the table called user when the structure is something like this.



      user



       id name

      1 Alice
      2 Bob
      3 jhon


      userdata



       adder added
      1 2
      1 3


      the userdata table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata like



       1 2 i.e (**Alice added Bob**)


      and when there is second row populated from front end that says



       2 1 i.e (**Bob added Alice**)


      so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.










      share|improve this question













      I'v kind of login system where user add each other but when both users add each other we want to show them in different format/color.
      For example:A user is logged In and want to add others.
      If the currently logged in user adds someone into the system who has also added this currently logged In user then we want to show both of them in different format.



      Will become clear from the following scenario.



      I'v two table one is user user, and the second is userdata userdata.
      Column id is primary key in user and Foreign key in userdata as adder.
      I want to insert the data into the table called user when the structure is something like this.



      user



       id name

      1 Alice
      2 Bob
      3 jhon


      userdata



       adder added
      1 2
      1 3


      the userdata table shows that the user Alice has added Bob and Alice added jhon so whenever there is already a relation in userdata like



       1 2 i.e (**Alice added Bob**)


      and when there is second row populated from front end that says



       2 1 i.e (**Bob added Alice**)


      so when this happens i.e Alice added Bob and vice versa I want to insert them in to the DB and show them in different way.How to take decision when Alice Added Bob and Bob Added Alice.







      php mysql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 15:21









      Khan

      135




      135






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          A JOIN is what you are looking for.



          If you want to know who added who, you can use this query



          Schema (MySQL v5.7)



          CREATE TABLE users
          (
          id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
          name VARCHAR(255)
          );

          CREATE TABLE userdata
          (
          adder INT(6) NOT NULL,
          added INT(6) NOT NULL,
          CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
          CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
          PRIMARY KEY (adder, added)
          );

          INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");

          INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);



          Query #1



          SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
          FROM users aer
          INNER JOIN userdata ud ON aer.id = ud.adder
          INNER JOIN users aed ON aed.id = ud.added
          WHERE
          (
          SELECT COUNT(*)
          FROM userdata uda
          WHERE uda.adder = ud.added
          AND uda.added = ud.adder
          ) >= 1;


          Outputs :



          | Adder | Action | Added |
          | ----- | ------ | ----- |
          | Bob | added | Alice |
          | Alice | added | Bob |



          View on DB Fiddle






          share|improve this answer






















          • I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
            – Khan
            Nov 10 at 15:33










          • I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
            – Khan
            Nov 10 at 15:37










          • Oh I see, you might need a subquery then. Editing.
            – Cid
            Nov 10 at 15:38










          • @Khan : I updated my answer
            – Cid
            Nov 10 at 15:42










          • let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
            – Khan
            Nov 10 at 15:50











          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
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240365%2fhow-to-link-these-two-tables-in-sql%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



          accepted










          A JOIN is what you are looking for.



          If you want to know who added who, you can use this query



          Schema (MySQL v5.7)



          CREATE TABLE users
          (
          id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
          name VARCHAR(255)
          );

          CREATE TABLE userdata
          (
          adder INT(6) NOT NULL,
          added INT(6) NOT NULL,
          CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
          CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
          PRIMARY KEY (adder, added)
          );

          INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");

          INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);



          Query #1



          SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
          FROM users aer
          INNER JOIN userdata ud ON aer.id = ud.adder
          INNER JOIN users aed ON aed.id = ud.added
          WHERE
          (
          SELECT COUNT(*)
          FROM userdata uda
          WHERE uda.adder = ud.added
          AND uda.added = ud.adder
          ) >= 1;


          Outputs :



          | Adder | Action | Added |
          | ----- | ------ | ----- |
          | Bob | added | Alice |
          | Alice | added | Bob |



          View on DB Fiddle






          share|improve this answer






















          • I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
            – Khan
            Nov 10 at 15:33










          • I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
            – Khan
            Nov 10 at 15:37










          • Oh I see, you might need a subquery then. Editing.
            – Cid
            Nov 10 at 15:38










          • @Khan : I updated my answer
            – Cid
            Nov 10 at 15:42










          • let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
            – Khan
            Nov 10 at 15:50















          up vote
          0
          down vote



          accepted










          A JOIN is what you are looking for.



          If you want to know who added who, you can use this query



          Schema (MySQL v5.7)



          CREATE TABLE users
          (
          id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
          name VARCHAR(255)
          );

          CREATE TABLE userdata
          (
          adder INT(6) NOT NULL,
          added INT(6) NOT NULL,
          CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
          CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
          PRIMARY KEY (adder, added)
          );

          INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");

          INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);



          Query #1



          SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
          FROM users aer
          INNER JOIN userdata ud ON aer.id = ud.adder
          INNER JOIN users aed ON aed.id = ud.added
          WHERE
          (
          SELECT COUNT(*)
          FROM userdata uda
          WHERE uda.adder = ud.added
          AND uda.added = ud.adder
          ) >= 1;


          Outputs :



          | Adder | Action | Added |
          | ----- | ------ | ----- |
          | Bob | added | Alice |
          | Alice | added | Bob |



          View on DB Fiddle






          share|improve this answer






















          • I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
            – Khan
            Nov 10 at 15:33










          • I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
            – Khan
            Nov 10 at 15:37










          • Oh I see, you might need a subquery then. Editing.
            – Cid
            Nov 10 at 15:38










          • @Khan : I updated my answer
            – Cid
            Nov 10 at 15:42










          • let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
            – Khan
            Nov 10 at 15:50













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          A JOIN is what you are looking for.



          If you want to know who added who, you can use this query



          Schema (MySQL v5.7)



          CREATE TABLE users
          (
          id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
          name VARCHAR(255)
          );

          CREATE TABLE userdata
          (
          adder INT(6) NOT NULL,
          added INT(6) NOT NULL,
          CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
          CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
          PRIMARY KEY (adder, added)
          );

          INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");

          INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);



          Query #1



          SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
          FROM users aer
          INNER JOIN userdata ud ON aer.id = ud.adder
          INNER JOIN users aed ON aed.id = ud.added
          WHERE
          (
          SELECT COUNT(*)
          FROM userdata uda
          WHERE uda.adder = ud.added
          AND uda.added = ud.adder
          ) >= 1;


          Outputs :



          | Adder | Action | Added |
          | ----- | ------ | ----- |
          | Bob | added | Alice |
          | Alice | added | Bob |



          View on DB Fiddle






          share|improve this answer














          A JOIN is what you are looking for.



          If you want to know who added who, you can use this query



          Schema (MySQL v5.7)



          CREATE TABLE users
          (
          id INT(6) AUTO_INCREMENT NOT NULL PRIMARY KEY,
          name VARCHAR(255)
          );

          CREATE TABLE userdata
          (
          adder INT(6) NOT NULL,
          added INT(6) NOT NULL,
          CONSTRAINT adder_user FOREIGN KEY (adder) REFERENCES users(id),
          CONSTRAINT added_user FOREIGN KEY (added) REFERENCES users(id),
          PRIMARY KEY (adder, added)
          );

          INSERT INTO users VALUES (default, "Alice"), (default, "Bob"), (default, "John");

          INSERT INTO userdata VALUES (1, 2), (1, 3), (2, 3), (2, 1);



          Query #1



          SELECT aer.name AS Adder, 'added' AS Action, aed.name AS Added
          FROM users aer
          INNER JOIN userdata ud ON aer.id = ud.adder
          INNER JOIN users aed ON aed.id = ud.added
          WHERE
          (
          SELECT COUNT(*)
          FROM userdata uda
          WHERE uda.adder = ud.added
          AND uda.added = ud.adder
          ) >= 1;


          Outputs :



          | Adder | Action | Added |
          | ----- | ------ | ----- |
          | Bob | added | Alice |
          | Alice | added | Bob |



          View on DB Fiddle







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 15:42

























          answered Nov 10 at 15:26









          Cid

          2,58811022




          2,58811022











          • I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
            – Khan
            Nov 10 at 15:33










          • I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
            – Khan
            Nov 10 at 15:37










          • Oh I see, you might need a subquery then. Editing.
            – Cid
            Nov 10 at 15:38










          • @Khan : I updated my answer
            – Cid
            Nov 10 at 15:42










          • let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
            – Khan
            Nov 10 at 15:50

















          • I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
            – Khan
            Nov 10 at 15:33










          • I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
            – Khan
            Nov 10 at 15:37










          • Oh I see, you might need a subquery then. Editing.
            – Cid
            Nov 10 at 15:38










          • @Khan : I updated my answer
            – Cid
            Nov 10 at 15:42










          • let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
            – Khan
            Nov 10 at 15:50
















          I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
          – Khan
          Nov 10 at 15:33




          I want to display only those users who are added by each other i.e if Alice added Bob and vice versa. But I don't know how to do something so that to know that yes! Alice has added Bob so when Bob is adding her ,it must be going to display to the user?because they both are adding each other if just one is adding then don't show to the user @Cid
          – Khan
          Nov 10 at 15:33












          I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
          – Khan
          Nov 10 at 15:37




          I'm trying to solve this issue from last couple of hours but nothing is just happening cool, please sort out how to do this?? @Cid
          – Khan
          Nov 10 at 15:37












          Oh I see, you might need a subquery then. Editing.
          – Cid
          Nov 10 at 15:38




          Oh I see, you might need a subquery then. Editing.
          – Cid
          Nov 10 at 15:38












          @Khan : I updated my answer
          – Cid
          Nov 10 at 15:42




          @Khan : I updated my answer
          – Cid
          Nov 10 at 15:42












          let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
          – Khan
          Nov 10 at 15:50





          let me check!! but You have use 'aer' and 'aed', I don't know what is this used for?can you please elaborate ?@Cid
          – Khan
          Nov 10 at 15:50


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240365%2fhow-to-link-these-two-tables-in-sql%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Museum of Modern and Contemporary Art of Trento and Rovereto

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully