MySQL计数问题

时间:2015-12-27 17:42:47

标签: php mysql sql oscommerce

在我的应用程序(osCommerce)中,我不得不修改一个如下所示的查询:

SELECT     sql_cache distinct p.products_image, 
           p.products_subimage1, 
           pd.products_name, 
           p.products_quantity, 
           p.products_model, 
           p.products_ordered, 
           p.products_id, 
           p.products_price, 
           p.products_date_added, 
           p.products_weight, 
           p.products_length, 
           p.products_width, 
           p.products_height, 
           p.products_tax_class_id, 
           p.products_status, 
           IF(s.status, s.specials_new_products_price, NULL)             AS specials_new_products_price,
           IF(s.status, s.specials_new_products_price, p.products_price) AS final_price 
FROM       products p 
LEFT JOIN  specials s 
ON         p.products_id = s.products_id 
LEFT JOIN  products_to_categories p2c 
ON         p.products_id=p2c.products_id 
LEFT JOIN  products_description pd 
ON         p.products_id=pd.products_id 
INNER JOIN filter_association_products fap 
ON         p.products_id =fap.products_id 
LEFT JOIN  products_attributes pa 
ON         p.products_id = pa.products_id 
WHERE      p.products_status = '1' 
AND        date_sub(curdate(),INTERVAL 3000 day) <= p.products_date_added 
AND        find_in_set(fap.filter_id,'126, 130')
ORDER BY   p.products_date_added DESC, 
           pd.products_name

这样结束,以获得准确的结果:

AND      fap.filter_id IN (126, 130)
GROUP BY p.products_id
HAVING COUNT(DISTINCT fap.filter_id) = 2;

我现在遇到的问题是有以下查询正在使用这个新查询并给我错误的结果。

SELECT COUNT( DISTINCT p.products_id ) AS total
FROM products p
LEFT JOIN specials s ON p.products_id = s.products_id
LEFT JOIN products_to_categories p2c 
ON p.products_id = p2c.products_id
LEFT JOIN products_description pd 
ON p.products_id = pd.products_id
INNER JOIN filter_association_products fap ON p.products_id = fap.products_id
LEFT JOIN products_attributes pa 
ON p.products_id = pa.products_id
WHERE p.products_status =  '1'
AND DATE_SUB( CURDATE( ) , INTERVAL 3000 DAY ) <= p.products_date_added
AND fap.filter_id IN ( 126, 130 ) 
GROUP BY p.products_id
HAVING COUNT( DISTINCT fap.filter_id ) =2
ORDER BY p.products_date_added DESC , pd.products_name

而不是给出原始查询中所有product_id的计数的1行结果,现在给出多行的结果(与预期的总产品数相同的行数)每个人中的数字1。 主要问题似乎是GROUP BY p.products_id HAVING COUNT( DISTINCT fap.filter_id ) =2

有没有办法修改原始查询,以便在使用AND fap.filter_id IN ( 126, 130 )时计数查询能够正常工作?

2 个答案:

答案 0 :(得分:3)

您需要使用子查询来获得所需的结果:

select count(*) 
from (<old query here>) s;

您需要在产品ID级别进行汇总才能获得符合原始条件的产品。计算此类产品的数量需要另一个聚合。

答案 1 :(得分:1)

正如戈登所说,使用子查询通常应该有效。 由于此查询看起来像产品列表查询,并且由于您使用的是osCommerce,因此在修改查询以使用子查询时,您将遇到另一个核心问题: 拆分页面结果的计数查询将失败,因为它不支持子查询。 要解决此问题,您还需要修改catalog/includes/classes/split_page_results.php

中的核心代码

更改:

$count_query = tep_db_query("select count(" . $count_string . ") as total " . substr($this->sql_query, $pos_from, ($pos_to - $pos_from)));

要:

$count_query = tep_db_query("select count(*) as total from (" . $this->sql_query . ") AS derivedtable1");

此处有更多详情:http://forums.oscommerce.com/topic/290110-class-splitpageresults/