MySQL select all records that match the same others records in another table











up vote
0
down vote

favorite
1












I have two tables:
Table _models with fields name and model_id
And
Table _tags with fields tag_name, tags_id and model_id



In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.



How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?



For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I have two tables:
    Table _models with fields name and model_id
    And
    Table _tags with fields tag_name, tags_id and model_id



    In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.



    How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?



    For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have two tables:
      Table _models with fields name and model_id
      And
      Table _tags with fields tag_name, tags_id and model_id



      In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.



      How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?



      For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench










      share|improve this question













      I have two tables:
      Table _models with fields name and model_id
      And
      Table _tags with fields tag_name, tags_id and model_id



      In my web app, I can assign some tags to a model by adding records in table _tags with the model’s model_id related field.



      How can I SELECT from table _models just the models which have the same tags assigned in the _tags table?



      For example, I need to SELECT all the models that have assigned both the tag #jacket and the tag #trench







      mysql join






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 21:29









      fboc

      31




      31
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.



          SELECT m.model_id, m.name
          FROM models m
          INNER JOIN tags t
          ON m.model_id = t.model_id
          WHERE t.tag_name IN ('#jacket', '#trench')
          GROUP BY m.model_id, m.name
          HAVING COUNT(DISTINCT t.tag_name) = 2;





          share|improve this answer





















          • This is exactly the solution of my problem, thank you Joe!
            – fboc
            Nov 21 at 22:50










          • @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
            – Madhur Bhaiya
            Nov 22 at 5:02


















          up vote
          0
          down vote













          Slightly different way to do the same:



          SELECT
          m.model_id
          , m.name
          FROM
          _models m
          WHERE
          2 = (
          SELECT
          COUNT(*)
          FROM
          _tags t
          WHERE
          m.model_id = t.model_id
          AND t.tag_name IN ('#jacket', '#trench')
          )


          Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)



          Note 2: Do not forget to add index (tag_name, model_id) on table _tags






          share|improve this answer





















          • Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
            – fboc
            Nov 22 at 7:54











          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%2f53420717%2fmysql-select-all-records-that-match-the-same-others-records-in-another-table%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








          up vote
          0
          down vote



          accepted










          For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.



          SELECT m.model_id, m.name
          FROM models m
          INNER JOIN tags t
          ON m.model_id = t.model_id
          WHERE t.tag_name IN ('#jacket', '#trench')
          GROUP BY m.model_id, m.name
          HAVING COUNT(DISTINCT t.tag_name) = 2;





          share|improve this answer





















          • This is exactly the solution of my problem, thank you Joe!
            – fboc
            Nov 21 at 22:50










          • @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
            – Madhur Bhaiya
            Nov 22 at 5:02















          up vote
          0
          down vote



          accepted










          For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.



          SELECT m.model_id, m.name
          FROM models m
          INNER JOIN tags t
          ON m.model_id = t.model_id
          WHERE t.tag_name IN ('#jacket', '#trench')
          GROUP BY m.model_id, m.name
          HAVING COUNT(DISTINCT t.tag_name) = 2;





          share|improve this answer





















          • This is exactly the solution of my problem, thank you Joe!
            – fboc
            Nov 21 at 22:50










          • @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
            – Madhur Bhaiya
            Nov 22 at 5:02













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.



          SELECT m.model_id, m.name
          FROM models m
          INNER JOIN tags t
          ON m.model_id = t.model_id
          WHERE t.tag_name IN ('#jacket', '#trench')
          GROUP BY m.model_id, m.name
          HAVING COUNT(DISTINCT t.tag_name) = 2;





          share|improve this answer












          For your example, you can use a GROUP BY/HAVING along with a COUNT DISTINCT to find models that have both of the tags assigned.



          SELECT m.model_id, m.name
          FROM models m
          INNER JOIN tags t
          ON m.model_id = t.model_id
          WHERE t.tag_name IN ('#jacket', '#trench')
          GROUP BY m.model_id, m.name
          HAVING COUNT(DISTINCT t.tag_name) = 2;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 21:42









          Joe Stefanelli

          109k13190204




          109k13190204












          • This is exactly the solution of my problem, thank you Joe!
            – fboc
            Nov 21 at 22:50










          • @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
            – Madhur Bhaiya
            Nov 22 at 5:02


















          • This is exactly the solution of my problem, thank you Joe!
            – fboc
            Nov 21 at 22:50










          • @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
            – Madhur Bhaiya
            Nov 22 at 5:02
















          This is exactly the solution of my problem, thank you Joe!
          – fboc
          Nov 21 at 22:50




          This is exactly the solution of my problem, thank you Joe!
          – fboc
          Nov 21 at 22:50












          @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
          – Madhur Bhaiya
          Nov 22 at 5:02




          @fboc Please see: How to accept an answer for closure. You get points for it as well. Thanks :)
          – Madhur Bhaiya
          Nov 22 at 5:02












          up vote
          0
          down vote













          Slightly different way to do the same:



          SELECT
          m.model_id
          , m.name
          FROM
          _models m
          WHERE
          2 = (
          SELECT
          COUNT(*)
          FROM
          _tags t
          WHERE
          m.model_id = t.model_id
          AND t.tag_name IN ('#jacket', '#trench')
          )


          Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)



          Note 2: Do not forget to add index (tag_name, model_id) on table _tags






          share|improve this answer





















          • Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
            – fboc
            Nov 22 at 7:54















          up vote
          0
          down vote













          Slightly different way to do the same:



          SELECT
          m.model_id
          , m.name
          FROM
          _models m
          WHERE
          2 = (
          SELECT
          COUNT(*)
          FROM
          _tags t
          WHERE
          m.model_id = t.model_id
          AND t.tag_name IN ('#jacket', '#trench')
          )


          Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)



          Note 2: Do not forget to add index (tag_name, model_id) on table _tags






          share|improve this answer





















          • Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
            – fboc
            Nov 22 at 7:54













          up vote
          0
          down vote










          up vote
          0
          down vote









          Slightly different way to do the same:



          SELECT
          m.model_id
          , m.name
          FROM
          _models m
          WHERE
          2 = (
          SELECT
          COUNT(*)
          FROM
          _tags t
          WHERE
          m.model_id = t.model_id
          AND t.tag_name IN ('#jacket', '#trench')
          )


          Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)



          Note 2: Do not forget to add index (tag_name, model_id) on table _tags






          share|improve this answer












          Slightly different way to do the same:



          SELECT
          m.model_id
          , m.name
          FROM
          _models m
          WHERE
          2 = (
          SELECT
          COUNT(*)
          FROM
          _tags t
          WHERE
          m.model_id = t.model_id
          AND t.tag_name IN ('#jacket', '#trench')
          )


          Note 1: You would better move tag names to separate table. So 3 tables: models (id, name), tags (id, name), tags2models (tag_id, model_id)



          Note 2: Do not forget to add index (tag_name, model_id) on table _tags







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 0:52









          fifonik

          26515




          26515












          • Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
            – fboc
            Nov 22 at 7:54


















          • Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
            – fboc
            Nov 22 at 7:54
















          Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
          – fboc
          Nov 22 at 7:54




          Thank you fifonik! I agree with you! In this case, I wrote just an example of my models and tags tables. In the project I have also a separate table for tags name.
          – fboc
          Nov 22 at 7:54


















          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%2f53420717%2fmysql-select-all-records-that-match-the-same-others-records-in-another-table%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)