How to retrieve intersecting values in the same table efficiently?












1















Assume i have the following table, 'some_table', which looks like this,



email | friend_email |
----------------------
s1 | f1 |
----------------------
s2 | f1 |
----------------------
s1 | f2 |
----------------------
s2 | f2 |
----------------------
s3 | f2 |
----------------------
s1 | f3 |
----------------------
s2 | f3 |
----------------------
s4 | f3 |


I then want to find the distinct and common values under 'email' between f1, f2 and f3.
The result returned should only be s1 and s2.



The SQL statement used will look like this,



SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f1') AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f2')) AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f3'));


The problem with this is it will get very lengthy when i have more friend_emails to add in.



In my node.js code, i want to use '?' (i think they are called constants ?) in the sql statements but i haven't succeeded. A sample of the code is below,



var getCommonFriends = "SELECT DISTINCT email FROM some_table WHERE friend_email = '?'";
var getCommonFriendsAppend = " AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email = '?')";

var friends = ["f1", "f2", "f3"];

var combinedSqlStatement = getCommonFriends;
var totalFriends = friends.length;
for(var i = 0; i < totalFriends; i++){
combinedSqlStatement = combinedSqlStatement + getCommonFriendsAppend;
}


My question is how do i come up with an sql statement that does the above and uses constants aka '?'










share|improve this question



























    1















    Assume i have the following table, 'some_table', which looks like this,



    email | friend_email |
    ----------------------
    s1 | f1 |
    ----------------------
    s2 | f1 |
    ----------------------
    s1 | f2 |
    ----------------------
    s2 | f2 |
    ----------------------
    s3 | f2 |
    ----------------------
    s1 | f3 |
    ----------------------
    s2 | f3 |
    ----------------------
    s4 | f3 |


    I then want to find the distinct and common values under 'email' between f1, f2 and f3.
    The result returned should only be s1 and s2.



    The SQL statement used will look like this,



    SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f1') AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f2')) AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f3'));


    The problem with this is it will get very lengthy when i have more friend_emails to add in.



    In my node.js code, i want to use '?' (i think they are called constants ?) in the sql statements but i haven't succeeded. A sample of the code is below,



    var getCommonFriends = "SELECT DISTINCT email FROM some_table WHERE friend_email = '?'";
    var getCommonFriendsAppend = " AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email = '?')";

    var friends = ["f1", "f2", "f3"];

    var combinedSqlStatement = getCommonFriends;
    var totalFriends = friends.length;
    for(var i = 0; i < totalFriends; i++){
    combinedSqlStatement = combinedSqlStatement + getCommonFriendsAppend;
    }


    My question is how do i come up with an sql statement that does the above and uses constants aka '?'










    share|improve this question

























      1












      1








      1








      Assume i have the following table, 'some_table', which looks like this,



      email | friend_email |
      ----------------------
      s1 | f1 |
      ----------------------
      s2 | f1 |
      ----------------------
      s1 | f2 |
      ----------------------
      s2 | f2 |
      ----------------------
      s3 | f2 |
      ----------------------
      s1 | f3 |
      ----------------------
      s2 | f3 |
      ----------------------
      s4 | f3 |


      I then want to find the distinct and common values under 'email' between f1, f2 and f3.
      The result returned should only be s1 and s2.



      The SQL statement used will look like this,



      SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f1') AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f2')) AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f3'));


      The problem with this is it will get very lengthy when i have more friend_emails to add in.



      In my node.js code, i want to use '?' (i think they are called constants ?) in the sql statements but i haven't succeeded. A sample of the code is below,



      var getCommonFriends = "SELECT DISTINCT email FROM some_table WHERE friend_email = '?'";
      var getCommonFriendsAppend = " AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email = '?')";

      var friends = ["f1", "f2", "f3"];

      var combinedSqlStatement = getCommonFriends;
      var totalFriends = friends.length;
      for(var i = 0; i < totalFriends; i++){
      combinedSqlStatement = combinedSqlStatement + getCommonFriendsAppend;
      }


      My question is how do i come up with an sql statement that does the above and uses constants aka '?'










      share|improve this question














      Assume i have the following table, 'some_table', which looks like this,



      email | friend_email |
      ----------------------
      s1 | f1 |
      ----------------------
      s2 | f1 |
      ----------------------
      s1 | f2 |
      ----------------------
      s2 | f2 |
      ----------------------
      s3 | f2 |
      ----------------------
      s1 | f3 |
      ----------------------
      s2 | f3 |
      ----------------------
      s4 | f3 |


      I then want to find the distinct and common values under 'email' between f1, f2 and f3.
      The result returned should only be s1 and s2.



      The SQL statement used will look like this,



      SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f1') AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f2')) AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email IN ('f3'));


      The problem with this is it will get very lengthy when i have more friend_emails to add in.



      In my node.js code, i want to use '?' (i think they are called constants ?) in the sql statements but i haven't succeeded. A sample of the code is below,



      var getCommonFriends = "SELECT DISTINCT email FROM some_table WHERE friend_email = '?'";
      var getCommonFriendsAppend = " AND email in (SELECT DISTINCT email FROM some_table WHERE friend_email = '?')";

      var friends = ["f1", "f2", "f3"];

      var combinedSqlStatement = getCommonFriends;
      var totalFriends = friends.length;
      for(var i = 0; i < totalFriends; i++){
      combinedSqlStatement = combinedSqlStatement + getCommonFriendsAppend;
      }


      My question is how do i come up with an sql statement that does the above and uses constants aka '?'







      mysql node.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 4:02









      winhungwinhung

      3641216




      3641216
























          1 Answer
          1






          active

          oldest

          votes


















          2














          I think you might be able to do a simple aggregation query here:



          SELECT email
          FROM some_table
          WHERE friend_email IN ('f1', 'f2', 'f3')
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          This option aggregates by email item, and then asserts that a certain set of friends are all present/associated with that item. This solution scales well, because to expand to more friends, you only need to add another friend to the IN clause, and change the distinct count assertion at the end of the query.




          Demo



          Here is a version of the query which might be more friendly in Node JS:



          SELECT email
          FROM some_table
          WHERE friend_email IN
          (
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual
          )
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          Then, bind the literal values 'f1', 'f2', and 'f3' to the above query.




          Demo






          share|improve this answer


























          • Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

            – winhung
            Jan 1 at 4:28













          • @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

            – Tim Biegeleisen
            Jan 1 at 4:44











          • My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

            – winhung
            Jan 1 at 5:06













          • Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

            – winhung
            Jan 1 at 5:09











          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%2f53992928%2fhow-to-retrieve-intersecting-values-in-the-same-table-efficiently%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









          2














          I think you might be able to do a simple aggregation query here:



          SELECT email
          FROM some_table
          WHERE friend_email IN ('f1', 'f2', 'f3')
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          This option aggregates by email item, and then asserts that a certain set of friends are all present/associated with that item. This solution scales well, because to expand to more friends, you only need to add another friend to the IN clause, and change the distinct count assertion at the end of the query.




          Demo



          Here is a version of the query which might be more friendly in Node JS:



          SELECT email
          FROM some_table
          WHERE friend_email IN
          (
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual
          )
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          Then, bind the literal values 'f1', 'f2', and 'f3' to the above query.




          Demo






          share|improve this answer


























          • Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

            – winhung
            Jan 1 at 4:28













          • @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

            – Tim Biegeleisen
            Jan 1 at 4:44











          • My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

            – winhung
            Jan 1 at 5:06













          • Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

            – winhung
            Jan 1 at 5:09
















          2














          I think you might be able to do a simple aggregation query here:



          SELECT email
          FROM some_table
          WHERE friend_email IN ('f1', 'f2', 'f3')
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          This option aggregates by email item, and then asserts that a certain set of friends are all present/associated with that item. This solution scales well, because to expand to more friends, you only need to add another friend to the IN clause, and change the distinct count assertion at the end of the query.




          Demo



          Here is a version of the query which might be more friendly in Node JS:



          SELECT email
          FROM some_table
          WHERE friend_email IN
          (
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual
          )
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          Then, bind the literal values 'f1', 'f2', and 'f3' to the above query.




          Demo






          share|improve this answer


























          • Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

            – winhung
            Jan 1 at 4:28













          • @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

            – Tim Biegeleisen
            Jan 1 at 4:44











          • My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

            – winhung
            Jan 1 at 5:06













          • Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

            – winhung
            Jan 1 at 5:09














          2












          2








          2







          I think you might be able to do a simple aggregation query here:



          SELECT email
          FROM some_table
          WHERE friend_email IN ('f1', 'f2', 'f3')
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          This option aggregates by email item, and then asserts that a certain set of friends are all present/associated with that item. This solution scales well, because to expand to more friends, you only need to add another friend to the IN clause, and change the distinct count assertion at the end of the query.




          Demo



          Here is a version of the query which might be more friendly in Node JS:



          SELECT email
          FROM some_table
          WHERE friend_email IN
          (
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual
          )
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          Then, bind the literal values 'f1', 'f2', and 'f3' to the above query.




          Demo






          share|improve this answer















          I think you might be able to do a simple aggregation query here:



          SELECT email
          FROM some_table
          WHERE friend_email IN ('f1', 'f2', 'f3')
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          This option aggregates by email item, and then asserts that a certain set of friends are all present/associated with that item. This solution scales well, because to expand to more friends, you only need to add another friend to the IN clause, and change the distinct count assertion at the end of the query.




          Demo



          Here is a version of the query which might be more friendly in Node JS:



          SELECT email
          FROM some_table
          WHERE friend_email IN
          (
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual UNION ALL
          SELECT ? FROM dual
          )
          GROUP BY email
          HAVING COUNT(DISTINCT friend_email) = 3;


          Then, bind the literal values 'f1', 'f2', and 'f3' to the above query.




          Demo







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 4:42

























          answered Jan 1 at 4:17









          Tim BiegeleisenTim Biegeleisen

          226k1392145




          226k1392145













          • Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

            – winhung
            Jan 1 at 4:28













          • @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

            – Tim Biegeleisen
            Jan 1 at 4:44











          • My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

            – winhung
            Jan 1 at 5:06













          • Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

            – winhung
            Jan 1 at 5:09



















          • Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

            – winhung
            Jan 1 at 4:28













          • @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

            – Tim Biegeleisen
            Jan 1 at 4:44











          • My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

            – winhung
            Jan 1 at 5:06













          • Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

            – winhung
            Jan 1 at 5:09

















          Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

          – winhung
          Jan 1 at 4:28







          Hi, thanks for the suggestion but if i converted your statement to use in node.js, it won't work. The problem, i believe, is due to me wanting to use constants. Eg. "SELECT email FROM some_table WHERE friend_email IN ? GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" Doing so will give me the following error, "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''f1', 'f2', 'f3' GROUP BY ' at line 1"

          – winhung
          Jan 1 at 4:28















          @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

          – Tim Biegeleisen
          Jan 1 at 4:44





          @winhung I gave you a possible workaround to using WHERE IN problem with a prepared statement in Node.

          – Tim Biegeleisen
          Jan 1 at 4:44













          My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

          – winhung
          Jan 1 at 5:06







          My bad, i had some syntax error. Yes, your solution works. I used, "SELECT email FROM some_table WHERE friend_email IN (?) GROUP BY email HAVING COUNT(DISTINCT friend_email) = ?" I was missing the '()' for the first '?'.

          – winhung
          Jan 1 at 5:06















          Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

          – winhung
          Jan 1 at 5:09





          Thank you ! I have tested the first example you gave and it works. Thanks ! Happy new year !!

          – winhung
          Jan 1 at 5:09




















          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%2f53992928%2fhow-to-retrieve-intersecting-values-in-the-same-table-efficiently%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

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas

          Can't read property showImagePicker of undefined in react native iOS