简体   繁体   中英

Incorrect rows returned by webshop filter

I have a table called products with the fields ID, NAME, URL, BRAND Then I have a field called product_filter with the fields ID, PRODUCT_ID, TYPE, VALUE

Lets assume I have the following products table

1 | PRODUCT 1 | product-1 | Brand 1
2 | PRODUCT 2 | product-2 | Brand 2
3 | PRODUCT 3 | product-3 | Brand 1

and the product_filter table

1 | 1 | material | wool
2 | 1 | material | cotton
3 | 2 | material | cotton
4 | 3 | material | wool
5 | 1 | season | fall
5 | 2 | season | fall
5 | 1 | season | all

etc etc

Now when a customer is on the webpage and trying to filter on

material : wool, cotton season : fall

That my result is product 1 (has both materials and correct season) and product 2 (has 1 material and the correct season).

I tried this with a join

SELECT DISTINCT(shop_product.product_number), `shop_product`.`color_count`, `shop_product`.`category_id`, `shop_product`.`in_stock`, `shop_product`.`url_image`, `shop_product_description`.* FROM (`shop_product`) JOIN `shop_product_description` ON `shop_product`.`id` = `shop_product_description`.`product_id` JOIN `shop_category_description` ON `shop_product`.`category_id` = `shop_category_description`.`category_id` INNER JOIN `shop_filters` ON `shop_product`.`id` = `shop_filters`.`product_id` WHERE `shop_product`.`status` = 1 AND `shop_product_description`.`language_id` = '1' AND `shop_category_description`.`language_id` = '1' AND ( (shop_filters.type = '1' AND shop_filters.keyword = 'cotton') OR (shop_filters.type = '1' AND shop_filters.keyword = 'wool') ) AND (shop_filters.type = '2' AND shop_filters.keyword = 'fall') ORDER BY shop_product`.`url_image` asc, `shop_product_description`.`name` desc LIMIT 36    

I used Distinct to get 1 oer product back (because of product 1 having both materials).

But I get no results until I change

( (shop_filters.type = '1' AND shop_filters.keyword = 'cotton') OR (shop_filters.type = '1' AND shop_filters.keyword = 'wool') ) AND (shop_filters.type = '2' AND shop_filters.keyword = 'fall')    

to

( (shop_filters.type = '1' AND shop_filters.keyword = 'cotton') OR (shop_filters.type = '1' AND shop_filters.keyword = 'wool') ) OR (shop_filters.type = '2' AND shop_filters.keyword = 'fall')    

However my results are then products with Cotton or Wool OR fall. If someone have suggestions, idea's then please ...

You need to match up multiple times against the filters table, to check against each filter. Easiest it probably to do multiple joins.

SELECT DISTINCT(shop_product.product_number), shop_product.color_count, shop_product.category_id, shop_product.in_stock, shop_product.url_image, shop_product_description.* 
FROM (shop_product) 
JOIN shop_product_description ON shop_product.id = shop_product_description.product_id 
JOIN shop_category_description ON shop_product.category_id = shop_category_description.category_id 
INNER JOIN shop_filters sf1 ON shop_product.id = sf1.product_id 
INNER JOIN shop_filters sf2 ON shop_product.id = sf2.product_id 
WHERE shop_product.status = 1 AND shop_product_description.language_id = '1' 
AND shop_category_description.language_id = '1' 
AND ( (sf1.type = '1' AND shop_filters.sf1 = 'cotton') OR (sf1.type = '1' AND sf1.keyword = 'wool') ) 
AND (sf2.type = '2' AND sf2.keyword = 'fall') 
ORDER BY shop_product.url_image asc, shop_product_description.name desc LIMIT 36

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM