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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:30:30+00:00 2026-05-28T03:30:30+00:00

I am attempting to do a tag system for selecting products from a database.

  • 0

I am attempting to do a tag system for selecting products from a database. I have read that the best way to achieve this is via a many-to-many relationship as using LIKE ‘%tag%’ is going to get slow when there are a lot of records. I have also read this question where to match on multiple tags you have to do a join for each tag being requested.

I have 3 tables: shop_products, shop_categories and shop_products_categories. And I need to, for example, be able to find products that have both the tag “flowers” and “romance”.

SELECT p.sku, p.name, p.path FROM shop_products p
  LEFT JOIN shop_products_categories pc1 ON p.sku = pc1.product_sku
  LEFT JOIN shop_categories c1           ON pc1.category_id = c1.id
  LEFT JOIN shop_products_categories pc2 ON p.sku = pc2.product_sku
  LEFT JOIN shop_categories c2           ON pc2.category_id = c2.id
WHERE c1.path = 'flowers' AND c2.path = 'romance'

This is the demo query I’m currently building to check it works before coding the relevant PHP for it and it works. But is this really the best way to do this? I find it hard to believe there isn’t a better way to do this than to do a join for each tag searched.
Thanks for any advice. 🙂

  • 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-28T03:30:31+00:00Added an answer on May 28, 2026 at 3:30 am

    No need to do multiple joins. If you need to match all tags, you can use an IN clause with a subquery like this:

    select p.sku, p.name, p.path 
    from shop_products p
    where p.sku in (
        select pc.product_sku 
        from shop_products_categories pc 
        inner join shop_categories c on pc.category_id = c.id
        where c.path in ('flowers', 'romance')
        group by pc.product_sku
        having count(distinct c.path) = 2
    )
    

    Note that you will need to adjust the number 2 to be the number of unique tags you are matching on. Beware in case this is user-entered data and they enter the same tag twice.

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

Sidebar

Related Questions

Attempting to print out a list of values from 2 different variables that are
Attempting to use the data series from this example no longer passes the JSONLint
Some context here...I have a System.Windows.Window that is used to display a modal message
I am attempting to remove a class from an html tag using JavaScript but
I have an xml tag that needs to be formatted like so: <AddDealRequest xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
I'm attempting to provide a border for a select tag and have been pulling
I'm attempting to build a ASP.NET website using MSBuild - specifically the AspNetCompiler tag.
In Ruby on Rails, I'm attempting to update the innerHTML of a div tag
Attempting to deploy a MOSS solution to a UAT server from dev server for
Attempting to make a NSObject called 'Person' that will hold the login details for

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.