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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:15:08+00:00 2026-06-06T17:15:08+00:00

I have several tables, books, bookcategories, categories bookcategories is a join table to allow

  • 0

I have several tables, books, bookcategories, categories

bookcategories is a join table to allow the many to many relationship between books and categories.

I want to be able to run a category search on books such that the search returns one row per book even when the book has many categories.

Books

ID   | Title
1    | Once upon...
2    | How many...
3    | How much...

Categories

ID   | Category
1    | x
2    | y
3    | z

BookCategories

   BookId | CategoryId
     1    |     1
     1    |     2
     2    |     2
     2    |     3
     2    |     1
     3    |     1

I thought I could get away with this:

SELECT  b.Id,
    b.Title,
FROM    ( books b
    INNER JOIN bookcategories bc ON b.ID= bc.BookId
    )
WHERE (bc.categoryId =1 AND bc.categoryId=2)
GROUP BY b.Id, b.Title

but as soon as I add the AND, the query returns no rows. But that is the criteria I need to apply – I only want to return book rows where the book has both category 1 and 2 as above (i.e. not category 1 OR category 2)

Can’t help thinking that I am missing something quite basic. Can anyone help?

Do I need to change the structure of the tables or is there a way of achieving what I need.

Wing

  • 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-06-06T17:15:10+00:00Added an answer on June 6, 2026 at 5:15 pm

    This should return books that have both category one and two:

    select  b.Id
    ,       b.Title
    from    books b
    join    bookcategories bc
    on      bc.BookId = b.Id
    group by
             b.Id
    ,        b.Title
    having   sum(case when bc.categoryId = 1 then 1 end) > 0
             and sum(case when bc.categoryId = 2 then 1 end) > 0
    

    Alternatively, you can use a double exists clause:

    select  b.Id
    ,       b.Title
    from    books b
    where   exists
            (
            select  *
            from    bookcategories bc
            where   bc.BookId = b.Id
                    and bc.CategoryId = 1
            )
            and exists
            (
            select  *
            from    bookcategories bc
            where   bc.BookId = b.Id
                    and bc.CategoryId = 2
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, Books and Authors , with many-to-many relationship between them through
I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT
I have several tables that I want to put side by side, however when
Okay, so I have several tables Vote Contest ContestSegment ContestRegistration Submittable User The relationship
I have an n to m relationship between authors and books. There are two
I have several tables (to be exact, 7) tables I cross join in one
I have several tables which looks like these below: <table id=table1> <tr> <th>Item</th> <th>Price</th>
In an existing DB, we have several tables associated with a central USERS table.
I have several tables: table user: id firstname lastname 1 John Doe 2 Jane
I have two tables, Author and Book, where an author can have many books.

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.