How to write an sql statement that selects a number of rows with specific IDs?












1














I have a table that I would like to select a number of rows from using one sql statement. is this possible? If so, please could someone help me with the syntax for such a statement? I was going to retrieve the rows separately in a foreach loop (PHP).



// PHP
$USER_ID = user_2
$arraySearchRecords = [id_2, id_21, id_54, id_543];

foreach($arraySearchRecords as $docId){
$sql = "SELECT * FROM myTable WHERE userId='$USER_ID' AND documentId='$docId' LIMIT 1";
$result = $MYSQLI -> query($sql);

}









share|improve this question






















  • just read mysql WHERE IN()
    – Devsi Odedra
    Oct 24 at 8:51
















1














I have a table that I would like to select a number of rows from using one sql statement. is this possible? If so, please could someone help me with the syntax for such a statement? I was going to retrieve the rows separately in a foreach loop (PHP).



// PHP
$USER_ID = user_2
$arraySearchRecords = [id_2, id_21, id_54, id_543];

foreach($arraySearchRecords as $docId){
$sql = "SELECT * FROM myTable WHERE userId='$USER_ID' AND documentId='$docId' LIMIT 1";
$result = $MYSQLI -> query($sql);

}









share|improve this question






















  • just read mysql WHERE IN()
    – Devsi Odedra
    Oct 24 at 8:51














1












1








1







I have a table that I would like to select a number of rows from using one sql statement. is this possible? If so, please could someone help me with the syntax for such a statement? I was going to retrieve the rows separately in a foreach loop (PHP).



// PHP
$USER_ID = user_2
$arraySearchRecords = [id_2, id_21, id_54, id_543];

foreach($arraySearchRecords as $docId){
$sql = "SELECT * FROM myTable WHERE userId='$USER_ID' AND documentId='$docId' LIMIT 1";
$result = $MYSQLI -> query($sql);

}









share|improve this question













I have a table that I would like to select a number of rows from using one sql statement. is this possible? If so, please could someone help me with the syntax for such a statement? I was going to retrieve the rows separately in a foreach loop (PHP).



// PHP
$USER_ID = user_2
$arraySearchRecords = [id_2, id_21, id_54, id_543];

foreach($arraySearchRecords as $docId){
$sql = "SELECT * FROM myTable WHERE userId='$USER_ID' AND documentId='$docId' LIMIT 1";
$result = $MYSQLI -> query($sql);

}






php sql mysqli






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 24 at 8:22









dean

1118




1118












  • just read mysql WHERE IN()
    – Devsi Odedra
    Oct 24 at 8:51


















  • just read mysql WHERE IN()
    – Devsi Odedra
    Oct 24 at 8:51
















just read mysql WHERE IN()
– Devsi Odedra
Oct 24 at 8:51




just read mysql WHERE IN()
– Devsi Odedra
Oct 24 at 8:51












2 Answers
2






active

oldest

votes


















1














implode your array as comma separated values like "id_2","id_21","id_54","id_543" and use IN clause



$clause = implode(",",$arraySearchRecord);
$clause = rtrim(",",$clause); //this will remove the last , that remains in the string

$sql="SELECT * from myTable WHERE documentId IN ($clause)";


This will retrieve all the records that match one of the values in the in clause



See the manual for more details



This way you don't need to iterate the array with the loop and repeat the query several times






share|improve this answer































    1














    Follow the steps:
    1. Create string from array $arraySearchRecord using implode.
    2. Use WHERE IN() clause to search more than one condition.



       <?php
    $USER_ID = user_2
    $condition = implode(',',$arraySearchRecord);
    $condition = rtrim(',',$condition); //used to remove the last
    $sql='SELECT * from myTable WHERE userId = "$USER_ID" AND documentId IN ($clause)';
    $result = $MYSQLI -> query($sql);
    ?>





    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%2f52964150%2fhow-to-write-an-sql-statement-that-selects-a-number-of-rows-with-specific-ids%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      implode your array as comma separated values like "id_2","id_21","id_54","id_543" and use IN clause



      $clause = implode(",",$arraySearchRecord);
      $clause = rtrim(",",$clause); //this will remove the last , that remains in the string

      $sql="SELECT * from myTable WHERE documentId IN ($clause)";


      This will retrieve all the records that match one of the values in the in clause



      See the manual for more details



      This way you don't need to iterate the array with the loop and repeat the query several times






      share|improve this answer




























        1














        implode your array as comma separated values like "id_2","id_21","id_54","id_543" and use IN clause



        $clause = implode(",",$arraySearchRecord);
        $clause = rtrim(",",$clause); //this will remove the last , that remains in the string

        $sql="SELECT * from myTable WHERE documentId IN ($clause)";


        This will retrieve all the records that match one of the values in the in clause



        See the manual for more details



        This way you don't need to iterate the array with the loop and repeat the query several times






        share|improve this answer


























          1












          1








          1






          implode your array as comma separated values like "id_2","id_21","id_54","id_543" and use IN clause



          $clause = implode(",",$arraySearchRecord);
          $clause = rtrim(",",$clause); //this will remove the last , that remains in the string

          $sql="SELECT * from myTable WHERE documentId IN ($clause)";


          This will retrieve all the records that match one of the values in the in clause



          See the manual for more details



          This way you don't need to iterate the array with the loop and repeat the query several times






          share|improve this answer














          implode your array as comma separated values like "id_2","id_21","id_54","id_543" and use IN clause



          $clause = implode(",",$arraySearchRecord);
          $clause = rtrim(",",$clause); //this will remove the last , that remains in the string

          $sql="SELECT * from myTable WHERE documentId IN ($clause)";


          This will retrieve all the records that match one of the values in the in clause



          See the manual for more details



          This way you don't need to iterate the array with the loop and repeat the query several times







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 24 at 8:34

























          answered Oct 24 at 8:26









          Lelio Faieta

          4,05552345




          4,05552345

























              1














              Follow the steps:
              1. Create string from array $arraySearchRecord using implode.
              2. Use WHERE IN() clause to search more than one condition.



                 <?php
              $USER_ID = user_2
              $condition = implode(',',$arraySearchRecord);
              $condition = rtrim(',',$condition); //used to remove the last
              $sql='SELECT * from myTable WHERE userId = "$USER_ID" AND documentId IN ($clause)';
              $result = $MYSQLI -> query($sql);
              ?>





              share|improve this answer




























                1














                Follow the steps:
                1. Create string from array $arraySearchRecord using implode.
                2. Use WHERE IN() clause to search more than one condition.



                   <?php
                $USER_ID = user_2
                $condition = implode(',',$arraySearchRecord);
                $condition = rtrim(',',$condition); //used to remove the last
                $sql='SELECT * from myTable WHERE userId = "$USER_ID" AND documentId IN ($clause)';
                $result = $MYSQLI -> query($sql);
                ?>





                share|improve this answer


























                  1












                  1








                  1






                  Follow the steps:
                  1. Create string from array $arraySearchRecord using implode.
                  2. Use WHERE IN() clause to search more than one condition.



                     <?php
                  $USER_ID = user_2
                  $condition = implode(',',$arraySearchRecord);
                  $condition = rtrim(',',$condition); //used to remove the last
                  $sql='SELECT * from myTable WHERE userId = "$USER_ID" AND documentId IN ($clause)';
                  $result = $MYSQLI -> query($sql);
                  ?>





                  share|improve this answer














                  Follow the steps:
                  1. Create string from array $arraySearchRecord using implode.
                  2. Use WHERE IN() clause to search more than one condition.



                     <?php
                  $USER_ID = user_2
                  $condition = implode(',',$arraySearchRecord);
                  $condition = rtrim(',',$condition); //used to remove the last
                  $sql='SELECT * from myTable WHERE userId = "$USER_ID" AND documentId IN ($clause)';
                  $result = $MYSQLI -> query($sql);
                  ?>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 at 11:29









                  shaan

                  152




                  152










                  answered Oct 24 at 10:10









                  Shanteshwar Inde

                  408212




                  408212






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f52964150%2fhow-to-write-an-sql-statement-that-selects-a-number-of-rows-with-specific-ids%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

                      Bressuire

                      Vorschmack

                      Quarantine