N
N
No_Time2012-09-01 19:10:40
PostgreSQL
No_Time, 2012-09-01 19:10:40

How to count the most popular element in a table via join table?

There is a comment model and a hashtag model connected by a “many to many” relationship through the join table comments_hashtags with the comment_id and hashtag_id keys.
How to count the most popular hashtag? those who have the most? Whether it is possible to make it more or less regular means of active record'a? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
1nd1go, 2012-09-01
@No_Time

I also forgot order by. Try with limit
select * from (select hashtag_id, count(*) as cnt from comments_hashtags group by hashtag_id order by cnt) LIMIT 1

1
1nd1go, 2012-09-01
@1nd1go

select * from (select hashtag_id, count(*) from comments_hashtags group by hashtag_id) where ROWNUM <= 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question