【发布时间】:2014-09-15 08:37:42
【问题描述】:
我有一个小提琴:http://sqlfiddle.com/#!2/46a7b5/18
此请求返回所有属性。
| META_NAME | META_VALUE | COUNT |
|----------------|------------|-------|
| Car Type | Coupe | 2 |
| Car Type | Sedan | 1 |
| Color | Black | 1 |
| Color | Red | 1 |
| Color | White | 1 |
| Interior Color | Black | 2 |
| Interior Color | Grey | 1 |
| Make | BMW | 2 |
| Make | Honda | 1 |
| Model | 2Series | 1 |
| Model | 3Series | 1 |
| Model | Civic | 1 |
要获得搜索结果,我有以下要求:
SELECT meta_name, meta_value, COUNT(DISTINCT item_id) count
FROM meta m JOIN item_meta im
ON im.field_id = m.id
WHERE item_id IN
(
SELECT i.id
FROM item_meta im JOIN items i
ON im.item_id = i.id JOIN meta m
ON im.field_id = m.id
GROUP BY i.id
HAVING MAX(meta_name = 'Make' AND meta_value = 'BMW') = 1
AND MAX(meta_name = 'Car Type' AND meta_value = 'Coupe') = 1
)
GROUP BY meta_name, meta_value;
我的输出:
| META_NAME | META_VALUE | COUNT |
|----------------|------------|-------|
| Car Type | Coupe | 2 |
| Color | Black | 1 |
| Color | White | 1 |
| Interior Color | Black | 1 |
| Interior Color | Grey | 1 |
| Make | BMW | 2 |
| Model | 2Series | 1 |
| Model | 3Series | 1 |
我正在寻找获得如下结果的方法:
| META_NAME | META_VALUE | COUNT |
|----------------|------------|-------|
| Car Type | Coupe | 2 |
| Car Type | Sedan | 0 |
| Color | Black | 1 |
| Color | Red | 0 |
| Color | White | 1 |
| Interior Color | Black | 2 |
| Interior Color | Grey | 1 |
| Make | BMW | 2 |
| Make | Honda | 0 |
| Model | 2Series | 1 |
| Model | 3Series | 1 |
| Model | Civic | 0 |
有可能吗?谢谢!
【问题讨论】:
标签: php mysql sql facet having