Munez NS
Munez NS

Reputation: 1011

SELECT TOP N records by column value

For instance, in table id/article/view_counter I would like to select top 5 articles by view_counter.

I could SELECT *, order by view count and then take only first 5 when I loop through array but is there a way to do that directly in query?

Upvotes: 0

Views: 1357

Answers (1)

Hituptony
Hituptony

Reputation: 2860

I believe you want to limit your records

SELECT ID, Article, count(view_counter) from table group by 1,2 order by 3 limit 5

Not sure your table structure, if view_Counter is already aggregate, you would just take the count and the group by off...

SELECT * from table order by 3 limit 5

This is assuming the view counter is your third column

If not then you would use

SELECT * from table order by view_counter limit 5

Upvotes: 1

Related Questions