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

  • Home
  • SEARCH
  • 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 7196779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:50:35+00:00 2026-05-28T20:50:35+00:00

Consider this table ‘cat_post’: entry_id cat_id 2 2 2 4 2 76 3 2

  • 0

Consider this table ‘cat_post’:

entry_id    cat_id
2           2
2           4
2           76
3           2
3           4
3           76
4           2
4           76

I need to select the entries that have both a row with cat_id = 2 and one with cat_id = 4 (not all entries that have either 2 or 4), so this select:

select * from cat_post where cat_id IN (2,4)

will produce:

entry_id    cat_id
2           2
2           4
3           2
3           4
4           2

Which is not what I want. I need to get entries if they have both a row with 2 and one with 4, but not 2 or 4, like:

entry_id    cat_id
2           2
2           4
3           2
3           4

(without entry_id 4 in this case)

What query can I use to get this result?

  • 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-28T20:50:36+00:00Added an answer on May 28, 2026 at 8:50 pm

    You could use something like:

    select entry_id from cat_post where cat_id in (2,4)
      group by entry_id
      having count(*) = 2;
    

    This is extensible to more than two values in your “in” set, but will fail if (entry_id,cat_id) is not a unique key. If that’s not the case, use:

    select entry_id from cat_post where cat_id in (2,4)
      group by entry_id
      having count(distinct cat_id) = 2;
    

    Example:

    mysql> select * from t;
    +------+------+
    | e    | c    |
    +------+------+
    |    2 |    2 |
    |    2 |    4 |
    |    3 |    2 |
    |    3 |    4 |
    |    4 |    2 |
    |    5 |    4 |
    +------+------+
    6 rows in set (0.00 sec)
    
    mysql> select e from t where c in (2,4) group by e having count(*) = 2;
    +------+
    | e    |
    +------+
    |    2 |
    |    3 |
    +------+
    2 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this: //members is a table. String sql=select * from members; Query query =
Consider this query that uses SELECT * and 'appends' a calculated column: SELECT *,
i need a complex query: Consider this table: ID - field1 - field2 =================
Consider this scenario. We have an internal Rails 2 app that connects to a
I need to combine different row's values in table in oracle. For example, consider
This question requires some hypothetical background. Let's consider an employee table that has columns
please consider this image: I have a table like this: Age Active Men/Women -------------------------------------------------
please consider this image: I have a table like this: Age Active Men/Women -------------------------------------------------
Consider this table on SQL Server wordID aliasID value =========================== 0 0 'cat' 1
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int

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.