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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:57:52+00:00 2026-06-09T14:57:52+00:00

I’ve got a client that wants me to make a tagging system. I’ve got

  • 0

I’ve got a client that wants me to make a tagging system. I’ve got three tables: items, tags, and item_tag_assoc – the last one only serves to associate item_ids and tag_ids. Here’s the problem I’m having:

If the user requests included tags [1, 2, 3] and excluded tags [4, 5, 6], the result should be all items that have EVERY tag in [1, 2, 3] (not just one) and NO tags in [4, 5, 6]. How do I write a query to accomplish this?

I researched enough to figure out inner joins for the tag inclusion:

SELECT i.item_id, i.item_title FROM items AS i INNER JOIN tag_item_assoc AS tia1 ON (tia1.item_id = i.item_id AND tia1.tag_id = 1)

…and just chain on the same inner join pattern for as many tags as you want to include. It may be a little bulky, but users won’t be choosing more than 4 or 5 tags before they move on, so it’ll do.

I was really hoping that I could exclude the same way, and wrap everything into one query:

INNER JOIN tag_item_assoc AS tia2 ON (tia2.item_id = i.item_id AND tia2.tag_id!= 2)

But it became obvious very quickly that wasn’t going to work. I read a couple of articles that said LEFT OUTER JOINs could let me exclude while I include, but I couldn’t figure them out, mostly because of the stray WHERE clauses. Any permutation of LEFT OUTER JOINs and INNER JOINs I tried either yielded an error or very confusing results.

All that to say – does anyone here know how I can accomplish this? I apologize for not having any useful code examples to provide. I’m ok with starting from scratch if the INNER JOINs are an obstacle – I just need a way to accomplish multiple association inclusion and exclusion at the same time. Thanks in advance for the help and expertise!

  • 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-09T14:57:53+00:00Added an answer on June 9, 2026 at 2:57 pm

    Found the answer. Instead of adding INNER JOINs like so:

    INNER JOIN tag_item_assoc AS tia2 ON (tia2.item_id = i.item_id AND tia2.tag_id!= 2)

    I added a sub-query with a single inner join:

    WHERE i.item_id NOT IN ( SELECT i.item_id FROM items AS s INNER JOIN tag_item_assoc AS tia1 ON (tia1.item_id = i.item_id AND tia1.tag_id IN (4, 5, 6)))

    The sub-query creates a set of all items that have any of the tags in [4, 5, 6], and WHERE i.iten_id NOT IN eliminates any records that match that set.

    Thanks everyone for the effort! Hopefully this will help someone down the road.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.