K
K
k-22016-02-12 11:16:43
PHP
k-2, 2016-02-12 11:16:43

2 tables: Products, Parameters. How to make a request?

there are 2 tables:
product:
id
params:
id
id_product
params
(there can be: color, price, size, etc.)
value (values ​​for each parameter)
How to make a query, if you need, for example, find - id_product with colors blue, green , price from 50 to 100 and sizes from 39 to 41
------------------------------------- -------------------------------------------------- ----------------------------------
And the second version of the tables (what is the best way to organize the tables, according to the first or second options? ):
product:
id
price
params:
id
id_product
params
(there can be: color, size, etc.)
value (values ​​for each parameter)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kirill Arutyunov, 2016-02-12
@arutyunov

Read about the entity-attribute-value of the table
If your price for a product does not change depending on the color, size or any other parameter, then the price is stored in the products table.
products_params structure:

product_id | param  | value
1          | color  | green
1          | color  | blue
2          | color  | green
3          | color  | blue
1          | weight | 300

Don't forget about the primary key.
Well, a request to get all the colors of the product with id = 1:
select * from products_params where product_id = 1 and param = 'color'

T
tigra, 2016-02-12
@tigroid3

Isn't it easier if you make 1 table, where at once, for each product, there will be parameters?

id | color | price |  size  | weight  |  other_info

A
Aleksej, 2016-02-12
@Shwed_Berlin

In my opinion, both options are little different and both bad.
What prevents to make the table for each parameter? Or even put the parameters in the product table?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question