簡體   English   中英

SQL Count all Rows

[英]SQL Count all Rows

我需要計算查詢的計數,但是我遇到了一些問題。

原始查詢效果很好。
它給了我一行正確的計數(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`)

或任何其他解決方案,讓我回到正確的計數

帶走group by子句。

嘗試這個

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

當您擁有沒有group by的查詢時,它將隱式地創建整個結果的一組,並獲得該單個組的計數。

通過添加group by您為每個審核創建一個組,結果是每個組的計數。

只需從查詢中刪除group by子句,即可獲得整個結果的計數。

嘗試這個

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`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM