Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6153491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:06:08+00:00 2026-05-23T20:06:08+00:00

I have a flags_products table like below (many to many relationship between flags and

  • 0

I have a flags_products table like below (many to many relationship between flags and products),

flags_products : 

flag_id --  product_id  
1      --   1   
2      --   1  
3      --   1  
1      --   2  
2      --   2  
4      --   2

What is the SQL query to get rows that have both the flags (flag_id’s 1 and 2) associated with a product (product_id)? Obviously:

  SELECT * 
    FROM flags_products 
   WHERE flag_id = 1 
     AND flag_id = 2 
GROUP BY product_id;

…doesn’t work, and gives an empty set. So what would be the correct query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:06:09+00:00Added an answer on May 23, 2026 at 8:06 pm

    The exact approach that you should take will depend on your larger requirements. Here’s one possible solution:

    SELECT
        product_id
    FROM
        Some_Table
    WHERE
        flag_id IN (1, 2)
    GROUP BY
        product_id
    HAVING
        COUNT(*) = 2
    

    This works as long as you can’t have duplicates. If a product can be flagged twice with the same flag_id then you would need:

    SELECT
        product_id
    FROM
        Some_Table
    WHERE
        flag_id IN (1, 2)
    GROUP BY
        product_id
    HAVING
        COUNT(DISTINCT flag_id) = 2
    

    In both of these cases you’ll need the GROUP BY to match your column list. MySQL doesn’t require that in order for the query to run (a flaw IMO, but I’ll save that argument for another time ), but the results won’t be determinable for the columns not in the GROUP BY. You can also use the above to queries as subqueries which you can then join to another table or tables.

    If you know that it will always be exactly two flags for which you’re looking then you can use EXISTS:

    SELECT
        T1.product_id
    FROM
        Some_Table T1
    WHERE
        EXISTS (
                SELECT *
                FROM
                    Some_Table T2
                WHERE
                    T2.product_id = T1.product_id AND
                    T2.flag_id = 1
               ) AND
        EXISTS (
                SELECT *
                FROM
                    Some_Table T2
                WHERE
                    T2.product_id = T1.product_id AND
                    T2.flag_id = 2
                )
    

    Performance may not be very good though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have enum like this [Flags] public enum Key { None = 0, A
I have a field in a table which contains bitwise flags. Let's say for
I have created a Parcelable object below, my object contains a List of Products.
In my project, I have a list of products available in table-layout. I need
I have written a query which works great on my local SQL Server 2005.
I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All
how do i switch on an enum which have the flags attribute set (or
I have this enum: [Flags] public enum ExportFormat { None = 0, Csv =
i have this code: [Flags] public enum MyUriType { ForParse, ForDownload, Unknown } and
suppose I have an enum [Flags] public enum E { zero = 0, one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.