douxin8610 2014-10-27 14:40
浏览 28
已采纳

网店过滤器返回的行不正确

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 ...

  • 写回答

1条回答 默认 最新

  • dpbf62565 2014-10-27 15:33
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作