问题
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
- 8 GB
- 12 GB
PROCESSOR
- INTEL
- AMD A
- 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