GROUP BY and COUNT in PostgreSQL

前端 未结 4 879
小蘑菇
小蘑菇 2020-12-14 00:03

The query:

SELECT COUNT(*) as count_all, 
       posts.id as post_id 
FROM posts 
  INNER JOIN votes ON votes.post_id = posts.id 
GROUP BY posts.id;
<         


        
4条回答
  •  别那么骄傲
    2020-12-14 00:22

    Using OVER() and LIMIT 1:

    SELECT COUNT(1) OVER()
    FROM posts 
       INNER JOIN votes ON votes.post_id = posts.id 
    GROUP BY posts.id
    LIMIT 1;
    

提交回复
热议问题