Answer the question
In order to leave comments, you need to log in
How to create a Mysql query with a filter on one column of several values?
There is a simple code with a request (let's find out if there is a filter by parameters - categories, brand, filter):
SELECT COUNT(*) AS total FROM product_filter f
LEFT JOIN product p ON(f.product_id = p.product_id)
LEFT JOIN product_to_category c ON(p.product_id = c.product_id)
WHERE f.filter_id = '" . (int)$filter_id . "' AND c.category_id = '".(int)$category_id."' AND p.manufacturer_id = '".(int)$manufacturer_id."'"
Answer the question
In order to leave comments, you need to log in
Thanks to SharuPoNemnogu - your comment is somehow not marked as a decision ... I paint the finished version here.
IT HAPPENED SO!:
Considering that we initially do not know how many filters there will be in the request:
// $category_id = id категории;
// $manufacturer_id = id производителя
$value = implode(',', $val_str); //(массив id фильтров приводим к 'x,a,b,z.......'
$value_num = count($val_str); // считаем сколько фильтров - чтоб в группе sql указать
$query = "SELECT COUNT(DISTINCT p.product_id) AS total FROM product p
LEFT JOIN product_filter f ON(p.product_id = f.product_id)
LEFT JOIN product_to_category c ON(p.product_id = c.product_id)
WHERE f.filter_id in (". $value .") AND c.category_id = '".(int)$category_id."' AND p.manufacturer_id = '".(int)$manufacturer_id."'
GROUP BY p.product_id having count(f.filter_id) = '".$value_num."'";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question