J
J
jenya77712018-06-27 14:37:07
PostgreSQL
jenya7771, 2018-06-27 14:37:07

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

2 answer(s)
M
Max, 2018-06-27
@MaxDukov

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

D
DS1977, 2018-06-27
@DS1977

Look
here
. However, I myself use the link tables, as described above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question