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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:12:15+00:00 2026-06-04T05:12:15+00:00

I’m working on a search engine for an online library, but I’m kind of

  • 0

I’m working on a search engine for an online library, but I’m kind of stuck here. When searching for tags, OR searches (ie books with “tag1” OR “tag2”) work fine, but the AND searches are giving me some trouble.

The tables (and their columns) I use for this are:

books  | book_id, other_info
tagmap | map_id, book_id, tag_id
tags   | tag_id, tag_text

Since a bunch of other search options can be en/disabled by the user, the query is generated by PHP. When searching for books with the tags “tag1” AND “tag2”, the following query is generated:

SELECT DISTINCT b.book_id, b.other_info
FROM books b, tagmap tm, tags t
WHERE b.book_id = "NA"
OR ( (t.tag_text IN ("tag1", "tag2"))
      AND tm.tag_id = t.tag_id
      AND b.book_id = tm.book_id )
HAVING COUNT(tm.book_id)=2

The WHERE line (which doesn’t give any results) is there so that additional parameters may be strung to the query more easily. I know this can be handled a lot nicer, but for now that doesn’t matter.

When doing an OR search (same query but without the HAVING COUNT line), it returns the two books in the database that have either of those tags, but when searching for the one book in the database that has BOTH tags, it returns nothing.

What’s wrong with the query? Is this not the/a way to do it? What am I overlooking?

Thanks!

EDIT: As per request, the data from each table relating to the book that should be returned:

books table:
book_id     110

tagmap table:
book_id     110    110
tag_id      15     16

tags table:
tag_id      15     16
tag_text    tag1   tag2

SOLUTION: All I had to do was include

GROUP BY b.book_id

before the HAVING COUNT line. Simple as that. The answer provided by taz is also worth looking into, especially if you’re aiming for optimising your search queries.

  • 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-04T05:12:17+00:00Added an answer on June 4, 2026 at 5:12 am

    The comma separated list of tables in your FROM clause functions like an inner join, so your query is selecting all of the rows in the tagmaps table and the tags table that have the same tag ID, and of those rows, all of the rows from the books table and the tagmaps table that have the same book ID. The HAVING clause then requires that two rows be returned from that result set with the same book ID. There can only be one row in the books table with any given book ID (assuming book ID is the primary key of the books table), so this condition is never met.

    What you want is a join without the books table. You are looking for the same book ID appearing twice in the results of the OR clauses (I believe), so you don’t want to join the books table with those results because that will ensure you can never have the same book ID in the results more than once.

    Edit: conceptually, you are essentially combining two different things. You are looking for tags and tagmaps for the same book, and you are also getting the book info from each of those books. So you are actually pulling duplicate other_info data for every instance of the same book ID in the tagmaps table, and then using the distinct clause to reduce that duplicate data down to one row, because all you want is the book ID and other_info. I would consider using two queries or a subquery to do this. There may be other [better] ways as well. I’d have to play around with it to figure it out.

    For starters, try

    SELECT DISTINCT tm.book_id, b.other_info
    FROM tagmap tm inner join tags t
        on tm.tag_id = t.tag_id
      left join books b
        on tm.book_id = b.book_id
    HAVING count(tm.book_id) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.