我需要计算查询的计数,但是我遇到了一些问题。
原始查询效果很好 它给了我一行正确的计数(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`
第二个查询不是给我回一行,但是没有正确的计数(3200),而是3200行,每一行都有不同的计数。
我需要的是:
SELECT COUNT (*) FROM
( `second_query`)
或任何其他让我回到正确计数的解决方案
答案 0 :(得分:1)
删除group by子句。
答案 1 :(得分: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
答案 2 :(得分:0)
如果您的查询没有group by
,它将隐式地创建整个结果的一组,并获得该单个组的计数。
通过添加group by
,您为每次审核创建了一个组,结果就是每个组的计数。
只需从查询中删除group by
子句,即可获得整个结果的计数。
答案 3 :(得分: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`