How To Find The Highest Number Of Times A Value Is In Records?
For example, in a table I have created called 'likes', there are three fields: id (irrelevant), user id (the id of the user), and post id (the id of the post). Say I have numerous
Solution 1:
It's a fairly simple aggregate function count() that will count the number of matching records.
select
postID,
count(*) as numLikes
from
yourTable
groupby
postID
orderby
numLikes desc
This will list the posts in order of the number of times they were liked.
Additionally, as I am providing you mainly a solution, you would do really well to read this Q&A that I wrote which will explain in a lot more detail how this all works - in fact I do a fair bit to cover off aggregate functions like this in it. I explain exactly what is going on in this sort of query around halfway down the first answer to the question :)
Post a Comment for "How To Find The Highest Number Of Times A Value Is In Records?"