M
M
masimka2017-11-17 17:18:25
PostgreSQL
masimka, 2017-11-17 17:18:25

How is it customary to implement the categories of manufacturers with the quantity of goods?

It is necessary to display the manufacturers in the category (eg refrigerator) to filter the goods in the online store., in the list of categories there must be a manufacturer, and the quantity of goods in this category, with this manufacturer.
How to display manufacturers in the list for the filter, with the quantity of goods?
Directed: only two tables, the first with manufacturers, the second with category ID (refrigerators, sporting goods.. ) and manufacturer ID, and the number of products in this category. One-to-many relationship.
Or in general, one table of manufacturers with a field for product categories in json format in which the category | (for example, a refrigerator, and quantity) is indicated.
a?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-11-17
@masimka

create table categories (
  id integer primary key,
  parent integer references categories,
  name varchar(50) not null
);

create table manufacturers (
  id integer primary key,
  name varchar(30) not null
);

create table goods (
  id integer primary key,
  category integer references categories,
  manufacturer integer references manufacturers,
  name varchar(50) not null
);

select
  m.name,
  count(*) as goods_count
from goods as g
inner join manufacturers as m
  on g.manufacturer = m.id
where category = 1
group by m.name;

Live example in sqlfiddle

A
Andrey Ezhgurov, 2017-11-17
@eandr_67

Wrong: not one-to-many, but many-to-many. Refrigerators are produced by more than one company. But firms produce not only refrigerators. And therefore there are 3 tables: categories, manufacturers and connection-category-manufacturer. This third table here contains a field for the number of products.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question