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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:59:32+00:00 2026-05-17T20:59:32+00:00

Alright well I recently got into normalizing my database for this little side project

  • 0

Alright well I recently got into normalizing my database for this little side project that I have been creating for a while now, but I’ve just hit a brick wall. I’ll try to give an understandable example of what I have and what I need to accomplish ― and hopefully it won’t be too painful. OK.

I have 3 tables the first one we will call Shows, structured something like this:

+----+--------------------------+
| id | title                    |
+----+--------------------------+
| 1  | Example #1               |
| 2  | Example #2               |
| 3  | Example #3               |
+----+--------------------------+

Plain and simple.

My next table is called Categories, and lookes like this:

+----+--------------------------+
| id | category                 |
+----+--------------------------+
| 1  | Comedy                   |
| 2  | Drama                    |
| 3  | Action                   |
+----+--------------------------+

And a final table called Show_categories:

+---------+---------+
| show_id | cat_id  |
+---------+---------+
| 1       |       1 |
| 1       |       3 |
| 2       |       2 |
| 2       |       3 |
| 3       |       1 |
| 3       |       2 |
+---------+---------+

As you may have noticed the problem is the in my database a single show can have multiple categories. Everything is structured fine, except for the fact that I can’t find a why to search for show with multiple categories.

If I were to search for action and comedy type shows I would be given Example #1, but it is not possible (at least with my queries), because the cat_id’s inside the Show_categories are in different rows.

Example of a working single category search (Selecting all comedy shows):

SELECT s.id,s.title
  FROM Shows s JOIN Show_categories sc ON sc.anid=s.id
  WHERE sc.cat_id=1 GROUP BY s.id

And a query that is impossible (because cat_id can’t equal 2 different things):

SELECT s.id,s.title
  FROM Shows s JOIN Show_categories sc ON sc.anid=s.id
  WHERE sc.cat_id=1 AND sc.cat_id=2 GROUP BY s.id

So to sum things up what I am asking is how do I handle a query where I am looking for a show based on multiple matching categories.

  • 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-17T20:59:33+00:00Added an answer on May 17, 2026 at 8:59 pm

    Use:

     SELECT s.id,
            s.title
        FROM SHOWS s 
        JOIN SHOW_CATEGORIES sc ON sc.anid = s.id
       WHERE sc.cat_id IN (1, 2) 
    GROUP BY s.id, s.title
      HAVING COUNT(DISTINCT sc.cat_id) = 2
    

    The COUNT(DISTINCT sc.cat_id) comparison needs to equal the number of cat_id values listed in the IN clause. But if both the SHOW_CATEGORIES show_id and cat_id columns are either the primary key, or there’s a unique constraint on both columns — then you can use COUNT(sc.cat_id).

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

Sidebar

Related Questions

Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
i've got regex which was alright, but as it camed out doesn't work well
Alright, currently I have my SWF hitting a php file that will go and
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright, I'm trying to read a comma delimited file and then put that into
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright I am working on a system for managing a bunch of vps. This
Alright so here it goes, I have my user system working perfectly, and now
Alright, first off I'm not quite sure how to phrase my problem. This could

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.