我有一个名为products的表,其中包含字段ID,NAME,URL,BRAND 然后我有一个名为product_filter的字段,其中包含字段ID,PRODUCT_ID,TYPE,VALUE
假设我有以下产品表
1 |产品1 |产品-1 |品牌1 2 |产品2 |产品-2 |品牌2 3 |产品3 |产品-3 |品牌1
和product_filter表
1 | 1 |材料|羊毛
2 | 1 |材料|棉花
3 | 2 |材料|棉花
4 | 3 |材料|羊毛
5 | 1 |季节|下跌
5 | 2 |季节|下跌
5 | 1 |季节|所有
等等
现在,当客户在网页上并尝试过滤
时 材料:羊毛,棉 季节:秋天我的结果是产品1(有材料和正确的季节)和产品2(有1种材料和正确的季节)。
我用连接尝试了这个
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
我使用Distinct获得1件产品(因为产品1有两种材料)。
但是在我改变之前我没有得到任何结果
( (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')
到
( (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')
然而,我的结果是棉花或羊毛的产品或跌落。 如果有人有建议,那么请想点......
答案 0 :(得分:1)
您需要多次匹配过滤器表,以检查每个过滤器。它最容易做多个连接。
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