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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:16:57+00:00 2026-05-17T01:16:57+00:00

I have a product_table with a product_id among several other fields. I am creating

  • 0

I have a product_table with a product_id among several other fields.

I am creating a tag_table with 2 fields, tag_id and tag_name.

The tags could be anything like "silver" or "penn state" etc.

I am also creating a product_tag_map table with product_id and tag_id in order to map a product to any number of tags.

If I wanted to create a category that contained all products tagged as "silver", "necklace", "diamond". But also exclude any product tagged as "initial", "journey"

(Obviously I would use the tag_id not tag_name, so products with tags [2,4,5] that do not have tags [3,6])

How could a create a temporary product table and populate it with the matching products?

update

here is my product_tag_map table:

CREATE TABLE `product_tag_map` (
 `product_tag_map_id` int(11) NOT NULL auto_increment,
 `tag_id` int(11) NOT NULL,
 `product_id` int(11) NOT NULL,
 PRIMARY KEY  (`product_tag_map_id`),
 UNIQUE KEY `tag_id` (`tag_id`,`product_id`),
 KEY `tag_id_2` (`tag_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7897 DEFAULT CHARSET=utf8

Note: I am not using product_tag_map_id for anything. I am just in the habit of giving each table a primary key like so. So If I should remove it thats fine.

  • 1 1 Answer
  • 1 View
  • 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-17T01:16:57+00:00Added an answer on May 17, 2026 at 1:16 am

    The question is, what would you need a temp table for? Why not query it directly?

    SELECT
      p.product_id,
      p.product_name
    FROM
      product_table p
    WHERE
      EXISTS (
        SELECT 1 
          FROM product_tag_map 
         WHERE product_id = p.product_id AND tag_id IN (2,4,5)
      )
      AND NOT EXISTS (
        SELECT 1 
          FROM product_tag_map 
         WHERE product_id = p.product_id AND tag_id IN (3,6)
      )
    

    Create appropriate indexes (one multi-column index over (product_tag_map.product_id, product_tag_map.tag_id) and one separate over (product_tag_map.tag_id), in addition to the “normal” PK/FK indexes) and this should be plenty fast.


    EDIT: A cacheable (as far as query plans go) and more dynamic variant of the above would be:

    Create a user_searches table (search_session_id, tag_id, include) with a multi_column index over (search_session_id, include) and a separate index over tag_id. Then fill it as the user selects criteria:

    search_session_id   tag_id   include
                  ...
                 4711        2         1
                 4711        4         1
                 4711        5         1
                 4711        3         0
                 4711        6         0
                  ...
    

    And query like this:

    SELECT
      p.product_id,
      p.product_name
    FROM
      product_table p
    WHERE
      EXISTS (
        SELECT 1 
          FROM product_tag_map m INNER JOIN user_searches s ON s.tag_id = m.tag_id
         WHERE m.product_id = p.product_id 
               AND s.search_session_id = 4711 /* this should be a parameter */
               AND s.include = 1
      )
      AND NOT EXISTS (
        SELECT 1 
          FROM product_tag_map m INNER JOIN user_searches s ON s.tag_id = m.tag_id
         WHERE m.product_id = p.product_id 
               AND s.search_session_id = 4711 /* this should be a parameter */
               AND s.include = 0
    
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table
I have an Order table like this: ORDER_ID PRODUCT_ID 1 1230 1 1231 1
I have to related tables like in the example script below: -- Creating table
We have three tables. 1) Product_Table Product_ID Product_Name 2) Warehouse1_Table Product_ID Inventory1 3) Warehouse2_Table
On my database I have among other tables, products . Products table contains the
I have the table (Product_Id, category priority, atribute1, atribute2...) in MS Access, and I
I have a question. I have here 2 tables table 1 : products product_id
I have a table with column for storing product code. | product_id | Product-name
Let's say I have a table: SELECT SUM(quantity) AS items_sold_since_date, product_ID FROM Sales WHERE
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this

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.