I have a database that looks pretty much like this:
I wanna make a MySQL query where I count the votes for each id and order them by starting from the highest. I want the output to be like:
Is it possible without making like 3 queries inside each other?
Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).
SQL SELECT COUNT() can be clubbed with SQL WHERE clause.
To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.
select
name,
sum(votes) as total_votes
from mytable
group by 1
order by 2 desc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With