How can I check if a value exists in another table's last N rows?











up vote
0
down vote

favorite
1












used contains a list of used items. I don't want the item to be used again if it was one of the last 10 items used.



From some googling, it seems like I need a subquery but can't get it to work. Here is what I tried so far



$check = mysqli_query($db, 'SELECT `id` 
FROM (
SELECT `id`
FROM `used`
ORDER BY `id` DESC
LIMIT 10)
WHERE `item` = ' . $id);


Before this I was using the following



$check = mysqli_query($db, 'SELECT `id` 
FROM `used`
WHERE `item` = ' . $id . '
ORDER BY `id` DESC
LIMIT 10);


However, seems like that was only limiting the results by 10, so would always return true if an item had been used, even outside of the last 10










share|improve this question




















  • 1




    See meta.stackoverflow.com/questions/333952/…
    – Strawberry
    Nov 10 at 1:09















up vote
0
down vote

favorite
1












used contains a list of used items. I don't want the item to be used again if it was one of the last 10 items used.



From some googling, it seems like I need a subquery but can't get it to work. Here is what I tried so far



$check = mysqli_query($db, 'SELECT `id` 
FROM (
SELECT `id`
FROM `used`
ORDER BY `id` DESC
LIMIT 10)
WHERE `item` = ' . $id);


Before this I was using the following



$check = mysqli_query($db, 'SELECT `id` 
FROM `used`
WHERE `item` = ' . $id . '
ORDER BY `id` DESC
LIMIT 10);


However, seems like that was only limiting the results by 10, so would always return true if an item had been used, even outside of the last 10










share|improve this question




















  • 1




    See meta.stackoverflow.com/questions/333952/…
    – Strawberry
    Nov 10 at 1:09













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





used contains a list of used items. I don't want the item to be used again if it was one of the last 10 items used.



From some googling, it seems like I need a subquery but can't get it to work. Here is what I tried so far



$check = mysqli_query($db, 'SELECT `id` 
FROM (
SELECT `id`
FROM `used`
ORDER BY `id` DESC
LIMIT 10)
WHERE `item` = ' . $id);


Before this I was using the following



$check = mysqli_query($db, 'SELECT `id` 
FROM `used`
WHERE `item` = ' . $id . '
ORDER BY `id` DESC
LIMIT 10);


However, seems like that was only limiting the results by 10, so would always return true if an item had been used, even outside of the last 10










share|improve this question















used contains a list of used items. I don't want the item to be used again if it was one of the last 10 items used.



From some googling, it seems like I need a subquery but can't get it to work. Here is what I tried so far



$check = mysqli_query($db, 'SELECT `id` 
FROM (
SELECT `id`
FROM `used`
ORDER BY `id` DESC
LIMIT 10)
WHERE `item` = ' . $id);


Before this I was using the following



$check = mysqli_query($db, 'SELECT `id` 
FROM `used`
WHERE `item` = ' . $id . '
ORDER BY `id` DESC
LIMIT 10);


However, seems like that was only limiting the results by 10, so would always return true if an item had been used, even outside of the last 10







php mysql sql database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 1:12









RiggsFolly

69k1764109




69k1764109










asked Nov 10 at 0:53









cantsay

76911026




76911026








  • 1




    See meta.stackoverflow.com/questions/333952/…
    – Strawberry
    Nov 10 at 1:09














  • 1




    See meta.stackoverflow.com/questions/333952/…
    – Strawberry
    Nov 10 at 1:09








1




1




See meta.stackoverflow.com/questions/333952/…
– Strawberry
Nov 10 at 1:09




See meta.stackoverflow.com/questions/333952/…
– Strawberry
Nov 10 at 1:09












3 Answers
3






active

oldest

votes

















up vote
2
down vote













It sounds like you want to check if the item $id was in the last 10 items in the used table. This query should do that:



$check = mysqli_query($db, "SELECT $id NOT IN (SELECT item 
FROM used
ORDER BY id DESC
LIMIT 10)");


This query will return 1 if the value of $id was not in the last 10 values of item in the used table, or 0 otherwise.



For versions of MySQL which don't support LIMIT in IN subqueries, you can use a LEFT JOIN as in this query which will return the same result:



SELECT IF(b.item IS NULL, 1, 0) 
FROM used a
LEFT JOIN (SELECT item
FROM used
ORDER BY id DESC LIMIT 10) b ON b.item = a.item
WHERE a.item = $id





share|improve this answer























  • Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
    – cantsay
    Nov 10 at 1:24












  • @cantsay I've added an alternate solution for earlier versions of MySQL
    – Nick
    Nov 10 at 1:55


















up vote
1
down vote













You can do it with a JOIN:



$check = mysqli_query($db, 'SELECT u.id
FROM used AS u
JOIN (
SELECT id
FROM used
ORDER BY id DESC LIMIT 10) AS ub
USING (id)
WHERE item = ' . $id);





share|improve this answer




























    up vote
    1
    down vote













    This works if your version does not support 'LIMIT & IN/ALL/ANY/SOME subquery":



    $check = mysqli_query($db, 'SELECT `item` FROM
    (SELECT `item`
    FROM `used`
    ORDER BY `id` DESC
    LIMIT 10) as useditems
    WHERE `item` = ' . $id );





    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',
      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%2f53235066%2fhow-can-i-check-if-a-value-exists-in-another-tables-last-n-rows%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      It sounds like you want to check if the item $id was in the last 10 items in the used table. This query should do that:



      $check = mysqli_query($db, "SELECT $id NOT IN (SELECT item 
      FROM used
      ORDER BY id DESC
      LIMIT 10)");


      This query will return 1 if the value of $id was not in the last 10 values of item in the used table, or 0 otherwise.



      For versions of MySQL which don't support LIMIT in IN subqueries, you can use a LEFT JOIN as in this query which will return the same result:



      SELECT IF(b.item IS NULL, 1, 0) 
      FROM used a
      LEFT JOIN (SELECT item
      FROM used
      ORDER BY id DESC LIMIT 10) b ON b.item = a.item
      WHERE a.item = $id





      share|improve this answer























      • Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
        – cantsay
        Nov 10 at 1:24












      • @cantsay I've added an alternate solution for earlier versions of MySQL
        – Nick
        Nov 10 at 1:55















      up vote
      2
      down vote













      It sounds like you want to check if the item $id was in the last 10 items in the used table. This query should do that:



      $check = mysqli_query($db, "SELECT $id NOT IN (SELECT item 
      FROM used
      ORDER BY id DESC
      LIMIT 10)");


      This query will return 1 if the value of $id was not in the last 10 values of item in the used table, or 0 otherwise.



      For versions of MySQL which don't support LIMIT in IN subqueries, you can use a LEFT JOIN as in this query which will return the same result:



      SELECT IF(b.item IS NULL, 1, 0) 
      FROM used a
      LEFT JOIN (SELECT item
      FROM used
      ORDER BY id DESC LIMIT 10) b ON b.item = a.item
      WHERE a.item = $id





      share|improve this answer























      • Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
        – cantsay
        Nov 10 at 1:24












      • @cantsay I've added an alternate solution for earlier versions of MySQL
        – Nick
        Nov 10 at 1:55













      up vote
      2
      down vote










      up vote
      2
      down vote









      It sounds like you want to check if the item $id was in the last 10 items in the used table. This query should do that:



      $check = mysqli_query($db, "SELECT $id NOT IN (SELECT item 
      FROM used
      ORDER BY id DESC
      LIMIT 10)");


      This query will return 1 if the value of $id was not in the last 10 values of item in the used table, or 0 otherwise.



      For versions of MySQL which don't support LIMIT in IN subqueries, you can use a LEFT JOIN as in this query which will return the same result:



      SELECT IF(b.item IS NULL, 1, 0) 
      FROM used a
      LEFT JOIN (SELECT item
      FROM used
      ORDER BY id DESC LIMIT 10) b ON b.item = a.item
      WHERE a.item = $id





      share|improve this answer














      It sounds like you want to check if the item $id was in the last 10 items in the used table. This query should do that:



      $check = mysqli_query($db, "SELECT $id NOT IN (SELECT item 
      FROM used
      ORDER BY id DESC
      LIMIT 10)");


      This query will return 1 if the value of $id was not in the last 10 values of item in the used table, or 0 otherwise.



      For versions of MySQL which don't support LIMIT in IN subqueries, you can use a LEFT JOIN as in this query which will return the same result:



      SELECT IF(b.item IS NULL, 1, 0) 
      FROM used a
      LEFT JOIN (SELECT item
      FROM used
      ORDER BY id DESC LIMIT 10) b ON b.item = a.item
      WHERE a.item = $id






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 10 at 1:54

























      answered Nov 10 at 1:15









      Nick

      20.6k51434




      20.6k51434












      • Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
        – cantsay
        Nov 10 at 1:24












      • @cantsay I've added an alternate solution for earlier versions of MySQL
        – Nick
        Nov 10 at 1:55


















      • Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
        – cantsay
        Nov 10 at 1:24












      • @cantsay I've added an alternate solution for earlier versions of MySQL
        – Nick
        Nov 10 at 1:55
















      Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
      – cantsay
      Nov 10 at 1:24






      Thanks. Can't get this to work due to the following, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery" but I think I found a fix for that, didn't know about (NOT) IN.
      – cantsay
      Nov 10 at 1:24














      @cantsay I've added an alternate solution for earlier versions of MySQL
      – Nick
      Nov 10 at 1:55




      @cantsay I've added an alternate solution for earlier versions of MySQL
      – Nick
      Nov 10 at 1:55












      up vote
      1
      down vote













      You can do it with a JOIN:



      $check = mysqli_query($db, 'SELECT u.id
      FROM used AS u
      JOIN (
      SELECT id
      FROM used
      ORDER BY id DESC LIMIT 10) AS ub
      USING (id)
      WHERE item = ' . $id);





      share|improve this answer

























        up vote
        1
        down vote













        You can do it with a JOIN:



        $check = mysqli_query($db, 'SELECT u.id
        FROM used AS u
        JOIN (
        SELECT id
        FROM used
        ORDER BY id DESC LIMIT 10) AS ub
        USING (id)
        WHERE item = ' . $id);





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          You can do it with a JOIN:



          $check = mysqli_query($db, 'SELECT u.id
          FROM used AS u
          JOIN (
          SELECT id
          FROM used
          ORDER BY id DESC LIMIT 10) AS ub
          USING (id)
          WHERE item = ' . $id);





          share|improve this answer












          You can do it with a JOIN:



          $check = mysqli_query($db, 'SELECT u.id
          FROM used AS u
          JOIN (
          SELECT id
          FROM used
          ORDER BY id DESC LIMIT 10) AS ub
          USING (id)
          WHERE item = ' . $id);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 1:27









          Antonio Alvarez

          10214




          10214






















              up vote
              1
              down vote













              This works if your version does not support 'LIMIT & IN/ALL/ANY/SOME subquery":



              $check = mysqli_query($db, 'SELECT `item` FROM
              (SELECT `item`
              FROM `used`
              ORDER BY `id` DESC
              LIMIT 10) as useditems
              WHERE `item` = ' . $id );





              share|improve this answer

























                up vote
                1
                down vote













                This works if your version does not support 'LIMIT & IN/ALL/ANY/SOME subquery":



                $check = mysqli_query($db, 'SELECT `item` FROM
                (SELECT `item`
                FROM `used`
                ORDER BY `id` DESC
                LIMIT 10) as useditems
                WHERE `item` = ' . $id );





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  This works if your version does not support 'LIMIT & IN/ALL/ANY/SOME subquery":



                  $check = mysqli_query($db, 'SELECT `item` FROM
                  (SELECT `item`
                  FROM `used`
                  ORDER BY `id` DESC
                  LIMIT 10) as useditems
                  WHERE `item` = ' . $id );





                  share|improve this answer












                  This works if your version does not support 'LIMIT & IN/ALL/ANY/SOME subquery":



                  $check = mysqli_query($db, 'SELECT `item` FROM
                  (SELECT `item`
                  FROM `used`
                  ORDER BY `id` DESC
                  LIMIT 10) as useditems
                  WHERE `item` = ' . $id );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 1:29









                  Manpreet

                  40016




                  40016






























                      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%2f53235066%2fhow-can-i-check-if-a-value-exists-in-another-tables-last-n-rows%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

                      Schultheiß

                      Verwaltungsgliederung Dänemarks

                      Liste der Kulturdenkmale in Wilsdruff