我有这些表
product_id name
51 a
52 b
53 c
54 s
55 e
product_id filter_id
52 11
51 22
51 33
52 33
54 11
filter_group_id filter_id name
1 11 white
1 22 black
2 33 formals
2 44 casuals
filter_group_id name
1 Colour
2 Style
我想要的是产品名称,颜色为黑色或白色,但样式应为正式。
**** ***** UPDATE
我想在此查询中集成查询:
SELECT DISTINCT oc_product.product_id, oc_product.image, oc_product.name, oc_product_option.option_id, oc_product.price,
ifnull((SELECT oc_product_special.price
FROM oc_product_special
WHERE oc_product_special.product_id = oc_product.product_id
AND '$date1' between oc_product_special.date_start and oc_product_special.date_end),0) as special_price,
ifnull((SELECT DISTINCT avg( oc_review.rating )
FROM oc_review
WHERE oc_review.product_id = oc_product.product_id
GROUP BY oc_product.product_id),0) as rating,
ifnull((SELECT DISTINCT count(
oc_review.product_id)
FROM oc_review
WHERE oc_review.product_id = oc_product.product_id
GROUP BY oc_product.product_id),0) as rated
FROM oc_product, oc_product_to_category, oc_product_option, oc_review $from
where oc_product_option.product_id = oc_product.product_id
AND oc_product_to_category.category_id = $category_id
AND oc_product_to_category.product_id = oc_product.product_id
SELECT DISTINCT oc_product.product_id, oc_product.image, oc_product.name, oc_product_option.option_id, oc_product.price,
ifnull((SELECT oc_product_special.price
FROM oc_product_special
WHERE oc_product_special.product_id = oc_product.product_id
AND '$date1' between oc_product_special.date_start and oc_product_special.date_end),0) as special_price,
ifnull((SELECT DISTINCT avg( oc_review.rating )
FROM oc_review
WHERE oc_review.product_id = oc_product.product_id
GROUP BY oc_product.product_id),0) as rating,
ifnull((SELECT DISTINCT count(
oc_review.product_id)
FROM oc_review
WHERE oc_review.product_id = oc_product.product_id
GROUP BY oc_product.product_id),0) as rated
FROM oc_product, oc_product_to_category, oc_product_option, oc_review $from
where oc_product_option.product_id = oc_product.product_id
AND oc_product_to_category.category_id = $category_id
AND oc_product_to_category.product_id = oc_product.product_id
答案 0 :(得分:1)
您可以在HAVING
子句中使用条件聚合:
SELECT p.name
FROM product p
JOIN product_filter pf
ON p.product_id = pf.product_id
JOIN filter_group_description fgd
ON pf.filter_id = fgd.filter_id
JOIN filter_group fg
ON fgd.filter_group_id = fg.filter_group_id
GROUP BY p.name
HAVING MAX(fgd.name = 'white')+MAX(fgd.name = 'black') > 0
AND MAX(fgd.name = 'formals') = 1
对于像fgd.name = 'white'
这样的真/假表达式,MySQL返回0或1,而其他数据库则必须使用MAX(CASE WHEN fgd.name = 'white' THEN 1 END)
。
演示:SQL Fiddle
编辑:如果您只想传递颜色列表和样式列表,可以在CASE
子句中使用带有IN
的{{1}}表达式,这样可以轻松实现使查询动态:
HAVING
动态处理新过滤器组会稍微复杂一些。
答案 1 :(得分:0)
SELECT p.name
FROM product p
JOIN
( SELECT pf.*,
fgd.filter_group_id,
fgd.name 'fgd_name',
fg.name 'fg_name'
FROM product_filter pf
JOIN filter_group_description fgd
ON pf.filter_id = fgd.filter_id
JOIN filter_group fg
ON fgd.filter_group_id = fg.filter_group_id
WHERE fg.name = 'Colour'
) filter1
ON p.product_id = filter1.product_id
JOIN
( SELECT pf.*,
fgd.filter_group_id,
fgd.name 'fgd_name',
fg.name 'fg_name'
FROM product_filter pf
JOIN filter_group_description fgd
ON pf.filter_id = fgd.filter_id
JOIN filter_group fg
ON fgd.filter_group_id = fg.filter_group_id
WHERE fg.name = 'Style'
) filter2
ON p.product_id = filter2.product_id
WHERE filter1.fgd_name IN
( 'black', 'white' )
AND filter2.fgd_name = 'formals'
这是一个SQLFiddle:http://sqlfiddle.com/#!9/8ab6e3/1