我有以下四个表,用于每个产品的产品过滤器场景。每个产品可以有多个属性,这些属性可以有多个值。
如何为多个属性搜索的用户请求构建 SQL 查询,例如
CPU: >1000
Price <700
我的最后一个 Jquery 是为了性能而损坏:
SELECT
attributes_vals.*, handys.*
FROM
handys join handy_has_vals on handys.id=handy_has_vals.handy_id
join attributes_vals on attributes_vals.id=handy_has_vals.val_id
WHERE
`attributes_vals`.`attr_id`=374
AND(`attributes_vals`.`vals` >=1000 AND `attributes_vals`.`vals` >=1500)
OR `attributes_vals`.`attr_id`=68 AND(`attributes_vals`.`vals`='YES')
OR `attributes_vals`.`attr_id`=11 AND( `attributes_vals`.`vals` LIKE 'GPRS%')
小提琴和测试:http: //SQLFiddle.com/# !2/bba51/2
这是表格
handys
id | name | modell
1 | Samsung | galaxy A
2 | Samsung | galaxy B
3 | Samsung | galaxy C
4 | Samsung | galaxy D
handy_has_vals
handy_id | attr_id | val_id
1 | 1 | 1
1 | 2 | 3
1 | 3 | 6
1 | 4 | 11
2 | 1 | 2
2 | 2 | 4
2 | 3 | 7
2 | 4 | 10
3 | 1 | 1
3 | 2 | 5
3 | 3 | 8
3 | 4 | 10
4 | 1 | 2
4 | 2 | 3
4 | 3 | 9
4 | 4 | 11
attributes_vals
id | attr_id | vals
1 | 1 | 'YES'
2 | 1 | 'No'
3 | 2 | '1200'
4 | 2 | '1000'
5 | 2 | '1500'
6 | 3 | '300'
7 | 3 | '350'
8 | 3 | '800'
9 | 3 | '550'
10 | 4 | 'YES'
11 | 4 | 'No'
attributes
id | attr_name
1 | 'GPS'
2 | 'CPU'
3 | 'Price'
4 | 'WLAN'