0

我需要计算查询的计数,但是我遇到了一些问题。

原始查询运行良好。
它给了我 1 行的正确计数(3200):

select count(e.entity_id) 
from `catalog_product_entity` AS `e`
INNER JOIN `review` AS `rt` 
ON rt.entity_pk_value = e.entity_id
INNER JOIN `review_detail` AS `rdt` 
ON rdt.review_id = rt.review_id
INNER JOIN `review_store` AS `store` 
ON rt.review_id=store.review_id AND store.store_id=0

我需要更改此查询以获取其他值,所以现在,这是新的计数查询:

select count(e.entity_id) 
from `catalog_product_entity` AS `e`
INNER JOIN `review` AS `rt` 
ON rt.entity_pk_value = e.entity_id
INNER JOIN `review_detail` AS `rdt` 
ON rdt.review_id = rt.review_id
LEFT JOIN `rating_option_vote` AS `vt` 
ON vt.review_id = rt.review_id
INNER JOIN `review_store` AS `store` 
ON rt.review_id=store.review_id AND store.store_id=0 
GROUP BY `rt`.`review_id`

第二个查询给我的不是 1 行具有正确计数 (3200),而是 3200 行,每行具有不同的计数。

我需要的是这样的:

SELECT COUNT (*) FROM
( `second_query`)

或任何其他让我恢复正确计数的解决方案

4

4 回答 4

1

去掉 group by 子句。

于 2013-04-03T12:14:07.443 回答
1

试试这个

select TOP 1 count(e.entity_id) OVER() [Total Count]
from `catalog_product_entity` AS `e`
INNER JOIN `review` AS `rt` 
ON rt.entity_pk_value = e.entity_id
INNER JOIN `review_detail` AS `rdt` 
ON rdt.review_id = rt.review_id
LEFT JOIN `rating_option_vote` AS `vt` 
ON vt.review_id = rt.review_id
INNER JOIN `review_store` AS `store` 
ON rt.review_id=store.review_id AND store.store_id=0 
GROUP BY `rt`.`review_id`

或者

SELECT COUNT(*)
FROM 
(select `rt`.`review_id`
from `catalog_product_entity` AS `e`
INNER JOIN `review` AS `rt` 
ON rt.entity_pk_value = e.entity_id
INNER JOIN `review_detail` AS `rdt` 
ON rdt.review_id = rt.review_id
LEFT JOIN `rating_option_vote` AS `vt` 
ON vt.review_id = rt.review_id
INNER JOIN `review_store` AS `store` 
ON rt.review_id=store.review_id AND store.store_id=0 
GROUP BY `rt`.`review_id`) AS T1
于 2013-04-03T12:24:22.610 回答
0

当您有没有 a 的查询时group by,它将隐式地将整个结果组成一组,并且您将获得该组的计数。

通过添加,group by您将为每个评论创建一个组,结果是每个组的计数。

只需group by从查询中删除该子句,您就可以获得整个结果的计数。

于 2013-04-03T12:16:25.143 回答
0

试试这个

select TOP 1 count(e.entity_id) OVER (partition by '1')
from `catalog_product_entity` AS `e`
INNER JOIN `review` AS `rt` 
ON rt.entity_pk_value = e.entity_id
INNER JOIN `review_detail` AS `rdt` 
ON rdt.review_id = rt.review_id
LEFT JOIN `rating_option_vote` AS `vt` 
ON vt.review_id = rt.review_id
INNER JOIN `review_store` AS `store` 
ON rt.review_id=store.review_id AND store.store_id=0 
GROUP BY `rt`.`review_id`
于 2013-04-03T12:37:26.883 回答