H
H
helixly2016-10-07 14:57:36
MySQL
helixly, 2016-10-07 14:57:36

SQL query for feature filter?

Good afternoon!
There is a table with product characteristics storage:
PropertyPivot
id
product_id
property_id
option_id
I actually have 2 questions. First, tell me what structure to use for multiple property values. Just create another such record in the database, with a different value?
And the second question, help with formulating an SQL query to filter by these characteristics. An example, if for example I choose Door - With a mirror and the outer color - Light, so that in the end only the door that is both with a mirror and white is selected, and not both.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-10-07
@helixly

SELECT `p`.*
  FROM `products` AS `p`
  JOIN `PropertyPivot` AS `pp1` ON `pp1`.`product_id` = `p`.`id`
    AND `pp1`.`property_id` = :p1_property AND `pp1`.`option_id` = :p1_value
  JOIN `PropertyPivot` AS `pp2` ON `pp2`.`product_id` = `p`.`id`
    AND `pp2`.`property_id` = :p2_property AND `pp2`.`option_id` = :p2_value

V
V Sh., 2016-10-07
@JuniorNoobie

I can offer three options:
1) We do everything with one request, something like:

SELECT product_id
FROM PropertyPivot
WHERE property_id IN (...) AND option_id IN (...)
GROUP BY product_id
HAVING count(*) = количество параметров поиска

2) Sequential screening of candidates for each parameter. For example, there was a search by three parameters. We first sample by one parameter, store intermediate data, then sample by intermediate data for the second parameter, and so on.
3) The most sophisticated option, I think. We build a pivot table according to the search parameters, connect it to the product table by product_id, add a condition and save the query...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question