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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:02:59+00:00 2026-06-13T19:02:59+00:00

I thought a query like this would be pretty easy because of the nature

  • 0

I thought a query like this would be pretty easy because of the nature of relational databases but it seems to be giving me a fit. I also searched around but found nothing that really helped. Here’s the situation:

Let’s say I have a simple relationship for products and product tags. This is a one-to-many relationship, so we could have the following:

productid  |  tag
========================
1          |  Car
1          |  Black
1          |  Ford
2          |  Car
2          |  Red
2          |  Ford
3          |  Car
3          |  Black
3          |  Lexus
4          |  Motorcycle
4          |  Black
5          |  Skateboard
5          |  Black
6          |  Skateboard
6          |  Green

What’s the most efficient way to query for all (Ford OR Black OR Skateboard) AND NOT (Motorcycles OR Green)? Another query I’m going to need to do is something like all (Car) or (Skateboard) or (Green AND Motorcycle) or (Red AND Motorcycle).

There are about 150k records in the products table and 600k records in the tags tables, so the query is going to need to be as efficient as possible. Here’s one query that I’ve been messing around with (example #1), but it seems to be taking about 4 seconds or so. Any help would be much appreciated.

SELECT p.productid
FROM   products p
       JOIN producttags tag1 USING (productid)
WHERE  p.active = 1
       AND tag1.tag IN ( 'Ford', 'Black', 'Skatebaord' )
       AND p.productid NOT IN (SELECT productid
                               FROM   producttags
                               WHERE  tag IN ( 'Motorcycle', 'Green' ));

 

Update

The quickest query I’ve found so far is something like this. It’s taking 100-200ms but it seems pretty inflexible and ugly. Basically I’m grabbing all products that match Ford, Black, or Skateboard. Them I’m concatenating all of the tags for those matched products into a colon-separated string and removing all products that match on :Green: AND :Motorcycle:. Any thoughts?

SELECT p.productid,
       Concat(':', Group_concat(alltags.tag SEPARATOR ':'), ':') AS taglist
FROM   products p
       JOIN producttags tag1 USING (productid)
       JOIN producttags alltags USING (productid)
WHERE  p.active = 1
       AND tag1.tag IN ( 'Ford', 'Black', 'Skateboard' )
GROUP  BY tag1.productid
HAVING ( taglist NOT LIKE '%:Motorcycle:%'
         AND taglist NOT LIKE '%:Green:%' ); 
  • 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-13T19:03:00+00:00Added an answer on June 13, 2026 at 7:03 pm

    I’d write the exclusion join with no subqueries:

    SELECT p.productid
    FROM   products p
    INNER JOIN producttags AS t ON p.productid = t.productid
    LEFT OUTER JOIN producttags AS x ON p.productid = x.productid 
           AND x.tag IN ('Motorcycle', 'Green')
    WHERE  p.active = 1
           AND t.tag IN ( 'Ford', 'Black', 'Skateboard' )
           AND x.productid IS NULL;
    

    Make sure you have an index on products over the two columns (active, productid) in that order.

    You should also have an index on producttags over the two columns (productid, tag) in that order.

    Another query I’m going to need to do is something like all (Car) or (Skateboard) or (Green AND Motorcycle) or (Red AND Motorcycle).

    Sometimes these complex conditions are hard for the MySQL optimizer. One common workaround is to use UNION to combine simpler queries:

    SELECT p.productid
    FROM   products p
    INNER JOIN producttags AS t1 ON p.productid = t1.productid
    WHERE  p.active = 1
       AND t1.tag IN ('Car', 'Skateboard')
    
    UNION ALL
    
    SELECT p.productid
    FROM   products p
    INNER JOIN producttags AS t1 ON p.productid = t1.productid
    INNER JOIN producttags AS t2 ON p.productid = t2.productid 
    WHERE  p.active = 1
       AND t1.tag IN ('Motorcycle')
       AND t2.tag IN ('Green', 'Red');
    

    PS: Your tagging table is not an Entity-Attribute-Value table.

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

Sidebar

Related Questions

Thought this would be pretty straight forward, but my value is remaining the same
I thought this would be fairly simple but it turns out not to work
I always thought that you could use OR in a LIKE statment to query
Thought my range of search options would easily find this. I wish to combine
I thought I had resolved this but I obviously haven't and was hoping someone
I thought I'd find more about this topic but I didn't. I have to
Given the following contrived example: I would like to query my data for all
I'm pretty new to ajax (via jQuery) and JavaScript. What I would like is
Seems like an obvious question but I'm still confused. I have an asp FileUpload
I have spent several hours with this SQL problem, which I thought would be

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.