Need help creating a query based on ui filter values

核能气质少年 提交于 2019-12-12 01:57:50

问题


It has been a few months now since the launch of our webshop and we are still creating new bling to make it easier for visitors to browse the catalog.

At the moment we are trying to add a filter that filters by product specifications.

Let's say the next options are available

MEMORY

  1. 8 GB
  2. 12 GB

PROCESSOR

  1. INTEL
  2. AMD A
  3. AMD E

The SQL to get all relevant product ID's looks like this at the moment:

SELECT `lu_specifications` . * , `app_products`.`id` AS product_id
FROM (
    `lu_specifications` 
)
LEFT OUTER JOIN `link_products_specifications` link_products_specifications ON `lu_specifications`.`id` = `link_products_specifications`.`specification_id` 
LEFT OUTER JOIN `app_products` app_products ON `app_products`.`id` = `link_products_specifications`.`product_id` 
LEFT OUTER JOIN `link_categories_products` product_link_categories_products ON `app_products`.`id` = `product_link_categories_products`.`product_id` 
WHERE `product_link_categories_products`.`category_id` =160

AND (
    `lu_specifications`.`id` = '60'
    AND `link_products_specifications`.`pres_value` = '8 GB'
)
AND (
    (
        `lu_specifications`.`id` = '54'
        AND `link_products_specifications`.`pres_value` = 'AMD A'
    )
    OR (
        `lu_specifications`.`id` = '54'
        AND `link_products_specifications`.`pres_value` = 'AMD E'
    )
)

In this example SQL the visitor would have selected filter options '8GB', 'AMD A' and 'AMD E'. So he would like to get a product which is/has (8 GB) AND ( (AMD A) OR (AMD E) ). Is this method correct? Because we are sure it should return like 17 products, but the SQL returns none, and it does not generate any error.

Could someone point us in the right direction please? :)


回答1:


Above query will for sure give no results. Because lu_specifications.id = '60' at one place and lu_specifications.id = '54' enclosed in AND condition. Its a column value, which can contain one value at a time, so both statements can not be TRUE together, which results in FALSE.

May be you want to use OR in following place. AND ( lu_specifications.id = '60' AND link_products_specifications.pres_value = '8 GB' ) OR ( ( lu_specifications.id = '54' AND link_products_specifications.pres_value = 'AMD A' ) OR ( lu_specifications.id = '54' AND link_products_specifications.pres_value = 'AMD E' ) )



来源:https://stackoverflow.com/questions/31403164/need-help-creating-a-query-based-on-ui-filter-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!