Answer the question
In order to leave comments, you need to log in
Assigning a product to multiple categories and searching through them PostgreSQL?
Hello, I have a table with a product and a table with categories (there is an id of the parent category), how can I specify several categories in the table with products, so that I can find them later. For example, a product has categories 4, 8, 12 and I need to get this product if I search in categories 8, 15 or 8, 12. The
question is what data type to use to store the category id and convenient search, as well as , so that it would be possible to use combined queries and get the product and all categories, including parent ones, in one query?
Answer the question
In order to leave comments, you need to log in
one product belongs to several categories and one category belongs to several products (many-to-many relationship). Need a 3rd link table.
I would store records in this 3rd
table id_product, id_category
in this case, all product categories are pulled out through
All products in a category are pulled through
SELECT id_product FROM prod_cat WHERE id_category=xx
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question