SQL SELECT non-distinct values where multiple rows match [duplicate]












-1
















This question already has an answer here:




  • Matching all values in IN clause

    2 answers




I have a junction table for many-many relationship that looks something like this:



Image_Tags:



Image_ID    |.    Tag_ID
4 | 5
4. | 6
4 | 7
5 | 6
5 | 7


I want create a SQL query to select an Image_ID that satisfies multiple Tag_ID requirements. For example, I want to find all Image_IDs that have both Tag_ID 5 AND 6, returning image 4 but not image 5. How would I go about this?










share|improve this question















marked as duplicate by philipxy, Community Nov 26 '18 at 16:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Hi. This is (obviously) a duplicate. (And shows no research effort & as an obvious faq it is not useful.) Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 26 '18 at 6:00













  • Why did you tag with relational algebra?

    – philipxy
    Nov 26 '18 at 6:02


















-1
















This question already has an answer here:




  • Matching all values in IN clause

    2 answers




I have a junction table for many-many relationship that looks something like this:



Image_Tags:



Image_ID    |.    Tag_ID
4 | 5
4. | 6
4 | 7
5 | 6
5 | 7


I want create a SQL query to select an Image_ID that satisfies multiple Tag_ID requirements. For example, I want to find all Image_IDs that have both Tag_ID 5 AND 6, returning image 4 but not image 5. How would I go about this?










share|improve this question















marked as duplicate by philipxy, Community Nov 26 '18 at 16:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Hi. This is (obviously) a duplicate. (And shows no research effort & as an obvious faq it is not useful.) Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 26 '18 at 6:00













  • Why did you tag with relational algebra?

    – philipxy
    Nov 26 '18 at 6:02
















-1












-1








-1









This question already has an answer here:




  • Matching all values in IN clause

    2 answers




I have a junction table for many-many relationship that looks something like this:



Image_Tags:



Image_ID    |.    Tag_ID
4 | 5
4. | 6
4 | 7
5 | 6
5 | 7


I want create a SQL query to select an Image_ID that satisfies multiple Tag_ID requirements. For example, I want to find all Image_IDs that have both Tag_ID 5 AND 6, returning image 4 but not image 5. How would I go about this?










share|improve this question

















This question already has an answer here:




  • Matching all values in IN clause

    2 answers




I have a junction table for many-many relationship that looks something like this:



Image_Tags:



Image_ID    |.    Tag_ID
4 | 5
4. | 6
4 | 7
5 | 6
5 | 7


I want create a SQL query to select an Image_ID that satisfies multiple Tag_ID requirements. For example, I want to find all Image_IDs that have both Tag_ID 5 AND 6, returning image 4 but not image 5. How would I go about this?





This question already has an answer here:




  • Matching all values in IN clause

    2 answers








mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '18 at 7:05









philipxy

11.8k42353




11.8k42353










asked Nov 26 '18 at 3:17









Sam GSam G

427




427




marked as duplicate by philipxy, Community Nov 26 '18 at 16:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by philipxy, Community Nov 26 '18 at 16:59


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Hi. This is (obviously) a duplicate. (And shows no research effort & as an obvious faq it is not useful.) Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 26 '18 at 6:00













  • Why did you tag with relational algebra?

    – philipxy
    Nov 26 '18 at 6:02





















  • Hi. This is (obviously) a duplicate. (And shows no research effort & as an obvious faq it is not useful.) Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

    – philipxy
    Nov 26 '18 at 6:00













  • Why did you tag with relational algebra?

    – philipxy
    Nov 26 '18 at 6:02



















Hi. This is (obviously) a duplicate. (And shows no research effort & as an obvious faq it is not useful.) Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

– philipxy
Nov 26 '18 at 6:00







Hi. This is (obviously) a duplicate. (And shows no research effort & as an obvious faq it is not useful.) Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on Minimal, Complete, and Verifiable example.

– philipxy
Nov 26 '18 at 6:00















Why did you tag with relational algebra?

– philipxy
Nov 26 '18 at 6:02







Why did you tag with relational algebra?

– philipxy
Nov 26 '18 at 6:02














1 Answer
1






active

oldest

votes


















2














You can use group by and having:



select image_id
from image_tags
where tag_id in (5, 6)
group by image_id
having count(*) = 2; -- "2" is the number of tags in the IN list





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can use group by and having:



    select image_id
    from image_tags
    where tag_id in (5, 6)
    group by image_id
    having count(*) = 2; -- "2" is the number of tags in the IN list





    share|improve this answer




























      2














      You can use group by and having:



      select image_id
      from image_tags
      where tag_id in (5, 6)
      group by image_id
      having count(*) = 2; -- "2" is the number of tags in the IN list





      share|improve this answer


























        2












        2








        2







        You can use group by and having:



        select image_id
        from image_tags
        where tag_id in (5, 6)
        group by image_id
        having count(*) = 2; -- "2" is the number of tags in the IN list





        share|improve this answer













        You can use group by and having:



        select image_id
        from image_tags
        where tag_id in (5, 6)
        group by image_id
        having count(*) = 2; -- "2" is the number of tags in the IN list






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 3:19









        Gordon LinoffGordon Linoff

        790k35314418




        790k35314418

















            Popular posts from this blog

            Wiesbaden

            To store a contact into the json file from server.js file using a class in NodeJS

            Marschland