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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:49:00+00:00 2026-05-18T12:49:00+00:00

I have two MySQL tables: product has id , title , price and some

  • 0

I have two MySQL tables:

  • product has id, title, price and some other product attributes
  • tag has *product_id* and *tag_name* fields

each product row can have one ore more rows in table tag joined by *product_id* field.
Question: how do I select products by *tag_name* and other attributes?
Example: I need to select all books cheaper than $10 and tagged ‘foreign’ and ‘sci-fi’

I know how to select products with one tag (join product and tag by *product_id* and set where clause)
I know how to select products with more than one tag using a stored procedure, however queries take very long time to run.
Is there a way to avoid stored procedure and still get the result?

Update: StackOverflow does something like this when you click “Related Tags” on the right hand side bar. Now, I don’t know what architechture their DB has, but the result is fairly quick selection of all questions tagged with multiple tags.

  • 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-18T12:49:01+00:00Added an answer on May 18, 2026 at 12:49 pm

    This will return any product with price less than 10, and with tag foreign or sci-fi

    How about something like

    SELECT  p.*
    FROM    product p
    WHERE   p.price < 10
    AND     EXISTS  (
                        SELECT  1
                        FROM    tag t
                        WHERE   p.id = t.product_id
                        AND     t.tag_name IN ('foreign','sci-fi')
                    )
    

    This will ensure that it has both tags, assuming that tags has primary key product_id and tag_name

    SELECT  p.*
    FROM    product p
    WHERE   p.price < 10
    AND     EXISTS  (
                        SELECT  1
                        FROM    tag t
                        WHERE   p.id = t.product_id
                        AND     t.tag_name IN ('foreign','sci-fi')
                        HAVING COUNT(1) = 2
                    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two MySQL tables (product and price history) that I would like to
In MySQL, I have two tables with a 1:n relationship. Table items has products,
I have two tables: Product: id | sku 0 P323 1 K534 Translation: product_id
I have two mysql tables called Product and Category Product id name category 1
I have two mySQL tables as follows: [product] table P_id | Name | Quantity
I have two tables: products products_tmp The products_tmp table has less fields. But all
I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required
I have data spread over two MySQL tables with different structures. One table has
i have two mysql tables tableA colA1 colA2 1 whatever 2 whatever 3 whatever
I have two MySQL tables, and I want to find and replace text strings

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.