1

我正在尝试将这组queries. 我已经完成了搜索,但我queries有一些joins,它变得越来越复杂,有人可以帮忙吗?

select count(DISTINCT p.products_id) as count  from (products p)
                            join (products_to_categories p2c)
                              on (p.products_id = p2c.products_id)
                            left join (specials s)
                              on (p.products_id = s.products_id)
                            left join (products_attributes pa)
                              on (p.products_id = pa.products_id)
                            left join (products_options_values pv)
                              on (pa.options_values_id = pv.products_options_values_id)
                            left join (products_stock ps)
                              on (p.products_id=ps.products_id and pv.products_options_values_id = ps.products_options_values_id2)        
                                                 INNER JOIN products_specifications ps10 ON p.products_id = ps10.products_id  INNER JOIN products_specifications ps17 ON p.products_id = ps17.products_id  where p.products_status = '1' and ps.products_stock_quantity>0   and p2c.categories_id in (72,1,23,47,48,49,74,24,33,34,35,77,25,46,45,44,40,41,42,43,76,78,83,50,52,81,79,2,54,60,82,53,57,58,55,61,62,63,75,56,64,65,66,67,68,69,70,71,84,80)  AND ps10.specification in ('Meisje') AND ps10.specifications_id = '10'
                      AND ps10.language_id = '1'
                       and products_options_values_name in ( 80,"18M/80cm","8-12 mnd","9m","12m","9-12m","9M","12M" )  AND ps17.specification in ('Overhemd', 'Polo') AND ps17.specifications_id = '17'
                      AND ps17.language_id = '1'

select count(DISTINCT p.products_id) as count  from (products p)
                            join (products_to_categories p2c)
                              on (p.products_id = p2c.products_id)
                            left join (specials s)
                              on (p.products_id = s.products_id)
                            left join (products_attributes pa)
                              on (p.products_id = pa.products_id)
                            left join (products_options_values pv)
                              on (pa.options_values_id = pv.products_options_values_id)
                            left join (products_stock ps)
                              on (p.products_id=ps.products_id and pv.products_options_values_id = ps.products_options_values_id2)        
                                                 INNER JOIN products_specifications ps10 ON p.products_id = ps10.products_id  INNER JOIN products_specifications ps17 ON p.products_id = ps17.products_id  where p.products_status = '1' and ps.products_stock_quantity>0   and p2c.categories_id in (72,1,23,47,48,49,74,24,33,34,35,77,25,46,45,44,40,41,42,43,76,78,83,50,52,81,79,2,54,60,82,53,57,58,55,61,62,63,75,56,64,65,66,67,68,69,70,71,84,80)  AND ps10.specification in ('Jongen') AND ps10.specifications_id = '10'
                      AND ps10.language_id = '1'
                       and products_options_values_name in ( 80,"18M/80cm","8-12 mnd","9m","12m","9-12m","9M","12M" )  AND ps17.specification in ('Overhemd', 'Polo') AND ps17.specifications_id = '17'
                      AND ps17.language_id = '1'

select count(DISTINCT p.products_id) as count  from (products p)
                            join (products_to_categories p2c)
                              on (p.products_id = p2c.products_id)
                            left join (specials s)
                              on (p.products_id = s.products_id)
                            left join (products_attributes pa)
                              on (p.products_id = pa.products_id)
                            left join (products_options_values pv)
                              on (pa.options_values_id = pv.products_options_values_id)
                            left join (products_stock ps)
                              on (p.products_id=ps.products_id and pv.products_options_values_id = ps.products_options_values_id2)        
                                                 INNER JOIN products_specifications ps10 ON p.products_id = ps10.products_id  INNER JOIN products_specifications ps17 ON p.products_id = ps17.products_id  where p.products_status = '1' and ps.products_stock_quantity>0   and p2c.categories_id in (72,1,23,47,48,49,74,24,33,34,35,77,25,46,45,44,40,41,42,43,76,78,83,50,52,81,79,2,54,60,82,53,57,58,55,61,62,63,75,56,64,65,66,67,68,69,70,71,84,80)  AND ps10.specification in ('Unisex') AND ps10.specifications_id = '10'
                      AND ps10.language_id = '1'
                       and products_options_values_name in ( 80,"18M/80cm","8-12 mnd","9m","12m","9-12m","9M","12M" )  AND ps17.specification in ('Overhemd', 'Polo') AND ps17.specifications_id = '17'
                      AND ps17.language_id = '1'
4

2 回答 2

0

好的,您的查询包含一些冗余,因为它们连接了在 select 和 where 子句中都未使用的表。我在下面的解决方案中删除了这些。我还重新组织了剩余的连接,将所有强制连接放在任何可选连接之前。

select ps10.specification, count(DISTINCT p.products_id) as [count]
from products p
  join products_to_categories p2c on p.products_id = p2c.products_id
  join products_specifications ps10 ON p.products_id = ps10.products_id  
  join products_specifications ps17 ON p.products_id = ps17.products_id  
  left join products_stock ps on p.products_id=ps.products_id and pv.products_options_values_id = ps.products_options_values_id2
where p.products_status = '1' and ps.products_stock_quantity > 0 
  and p2c.categories_id in (72,1,23,47,48,49,74,24,33,34,35,77,25,46,45,44,40,41,42,43,76,78,83,50,52,81,79,2,54,60,82,53,57,58,55,61,62,63,75,56,64,65,66,67,68,69,70,71,84,80)
  and ps10.specification in ('Meisje', 'Jongen', 'Unisex') 
  and ps10.specifications_id = '10'
  and ps10.language_id = '1'
  and products_options_values_name in ( 80,"18M/80cm","8-12 mnd","9m","12m","9-12m","9M","12M")
  and ps17.specification in ('Overhemd', 'Polo') 
  and ps17.specifications_id = '17'
  and ps17.language_id = '1' 
GROUP BY ps10.specification
ORDER BY ps10.specification

这将产生以下输出:

specification | count
Jongen          93
Maisje          245
Unisex          36

(好吧,数字可能有点假)

希望这可以帮助

于 2012-08-24T04:49:55.400 回答
0

我想出了这个,似乎工作

输出是

count1   count2   count3
------------------------
  10        0        2

@john bingham“您的查询包含一些冗余”

你能帮我减少这个查询的冗余吗?

select 
    count(DISTINCT if(ps10.specification in ('Meisje'),p.products_id,NULL)) as count1,
    count(DISTINCT if(ps10.specification in ('Jongen'),p.products_id,NULL)) as count2,
    count(DISTINCT if(ps10.specification in ('Unisex'),p.products_id,NULL)) as count3
from 
    (products p)
join 
    (products_to_categories p2c) on (p.products_id = p2c.products_id)
left join 
    (specials s) on (p.products_id = s.products_id)
left join 
    (products_attributes pa) on (p.products_id = pa.products_id)
left join 
    (products_options_values pv) on (pa.options_values_id = pv.products_options_values_id)
left join 
    (products_stock ps) on (p.products_id = ps.products_id 
                        and pv.products_options_values_id = ps.products_options_values_id2)       
inner join
    products_specifications ps10 ON p.products_id = ps10.products_id  
where 
    p.products_status = '1' 
    and ps.products_stock_quantity > 0   
    and p2c.categories_id in (72,1,23,47,48,49,74,24,33,34,35,77,25,46,45,44,40,41,42,43,76,78,83,50,52,81,79,2,54,60,82,53,57,58,55,61,62,63,75,56,64,65,66,67,68,69,70,71,84,80)  
    and ps10.specifications_id = '10'
    and ps10.language_id = '1'
于 2012-08-24T07:56:25.893 回答