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 7671675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:06:48+00:00 2026-05-31T16:06:48+00:00

I’d like to know the most efficient SQL query for achieving this problem: Say

  • 0

I’d like to know the most efficient SQL query for achieving this problem:

Say we have a table with two columns, one storing entry ids (entry_id) and one storing category ids (cat_id):

 entry_id       cat_id
 3              1
 3              2
 3              3
 3              20
 4              1
 4              2
 4              21

I’d like to count how many distinct entry_id‘s there are in the categories 1, 2 OR 3 but that also must be in cat_id 20.

For example, categories 1, 2 and 3 might represent music genres (Country, Pop etc.), while category 20 might be recording formats (CD, Vinyl etc.). So another way of putting it verbally could be: “How many products are there that are on Vinyl and in either the Pop or Country category?”

I could achieve this with a nested loop in code (PHP) or possibly with a nested SQL subquery, but neither feels that efficient. I feel there must be an obvious answer to this staring me in the face…

EDIT TO ADD:
I would also like to do this without modifying the database design, as it’s a third party system.

FURTHER EXAMPLE TO CLARIFY:
Another real-world example of why I’d need this data:

Let’s say the category ids instead represent either:

  • Accommodation Types (Camping = 20, Holiday Cottage = 21)

OR

  • Continents and their sub-regions (i.e. Europe = 1, UK = 2, England = 3)

Let’s say someone has selected that they are interested in camping (cat_id = 1). Now we need to count how many camping products there are in the Europe. A product might be tagged as both Europe (parent), UK (child) AND England (grand-child), giving us an array of category ids 1, 2 or 3. So we now need to count how many distinct products there are in both those categories AND the original accommodation category of 1 (camping).

So having selected Camping, the end result might look something like:

  • Europe: 4 camping products
    • UK: 2 camping products
      • England : 1 camping product
      • Wales : 1 camping product
    • France: 2 camping products
      etc.

Hope that helps…

  • 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-31T16:06:50+00:00Added an answer on May 31, 2026 at 4:06 pm

    I believe you want GROUP BY, COUNT() and EXISTS()

    declare @t table(entry_id int, cat_id int)
    
    insert @t select 1, 1
    insert @t select 2, 1
    insert @t select 1, 2
    insert @t select 2, 2
    insert @t select 3, 1
    insert @t select 1, 20
    
    select t1.cat_id, COUNT(*)
    from @t as t1
    where exists(
        select * from @t
        where t1.entry_id = entry_id 
        and cat_id = 20)
    group by t1.cat_id
    

    V2 using join instead of EXISTS()

    declare @t table(entry_id int, cat_id int)
    
    insert @t select 1, 1
    insert @t select 2, 1
    insert @t select 1, 2
    insert @t select 2, 2
    insert @t select 3, 1
    insert @t select 1, 20
    
    select t1.cat_id, COUNT(*)
    from @t as t1
    join @t as t2 on t1.entry_id = t2.entry_id and t2.cat_id = 20
    group by t1.cat_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.