How would I go along with fetching same name values from mysql - php

I have this table in mysql
I'm trying to select the usernames that has the most records in this database and display it on a website. So basically I want to see which username is logged the most and display that username along with the count. It's a voting system tracking the amount of people that vote. I would like to display the top five voters from this table. So the top 5 usernames that are repeated the most.
Therefore:
[4] davidxd33
will be displayed on the website because this user has four votes and is logged in the database four times.
I've tried SELECT username, count(username) FROM Votes which only returned the first name in the database, then the total count of usernames.

Try this:
SELECT username, COUNT(*)
FROM Votes
GROUP BY username
GROUP BY forces to return a line per username, and COUNT(*) counts the number of rows for each different username.

SELECT username,
count(*) as total
FROM Vote
group by username

Returns the top:
SELECT username, COUNT(*) AS total FROM Votes GROUP BY username ORDER BY total DESC
Thank you for the replies!

You should first group by usernames and then order them based on count in descending order and select the top 5. Hope this helps:-
select top 5 username, count(*) as votescount
from table t
group by username
order by votescount desc

Related

how do i count the amount of rows under a username?

I am making a forum and on the homepage I want to make some kind of a leaderboard where you can see the top 10 posters + and how much they have posted. I want to only get those users out of the database. Ill add a picture of my database. Please let me know.
Database
I don't know what you mean, but if you mean the database query, it can be something like this:
SELECT username,
COUNT(*) AS total_post
FROM your_table
GROUP BY username
ORDER BY COUNT(*) DESC
LIMIT 10
I assume your table name posts. You can replace it by your table name.
SELECT COUNT(post) as total_post, username FROM posts GROUP BY username;
It will return something like
total_post username
5 username

How do i calculate rank when i have multiple conditions in MySQL

I wanted to calculate rank of each unique username where the final score is the average of all the correct answers in various attempts by a user. Also each category should be kept different.
I saw about creating Temporary tables but the main problem is how do i write a query which would find unique usernames calculate their average and rank them.
Below is a screenshot of the Database.
You could use avg and group by
select username, avg(correct)
from my_table
group by username
of if you need for each category
select username, category, avg(correct)
from my_table
group by username, category

How to show online users in the top and offline after it?

Could you guys please tell me how to show all online people on the top and all offline after, both lists in ascending order of the username just like Skype does.
My MySQL table is Users, and the columns are as follow:
ID
Username
Last_logged_in - is with unix_timestamp();
the code I am using to show online people is as follow:
select * from users where Last_logged_in >= UNIX_TIMESTAMP() - 60;
and the code I tried was like so:
select id, username, last_logged_in from users where last_logged_in >= UNIX_TIMESTAMP() - 60 UNION select id, username, last_logged_in from users where last_logged_in <= UNIX_TIMESTAMP() - 60 LIMIT 10;
some helps will be really appreciated.
Your query doesn't provide the results you expect because UNION does not preserve the order of the rows from the queries it unions.
You need to enclose each of the unioned queries into parentheses and add ORDER BY after the last query. However, if you do that you obtain a big and slow query that selects all the records from table users in a convoluted way.
The solution is to order the rows descending by column Last_logged_in. This way, the users that logged in recently are returned first. Also you need to add in the fields list the expression that tells if the user is still online. Finally, because your query does not filter the returned users, a LIMIT clause is recommended; otherwise the query will return the entire table which is probably not what you want.
Update
As the OP specified in a comment, the users must be sorted by their online status (online first) and then by their usernames (ascending).
The (updated) query is:
SELECT u.*, IF(Last_logged_in >= UNIX_TIMESTAMP() - 60, 1, 0) as isOnline;
FROM users u
ORDER BY isOnline DESC, username ASC
LIMIT 20
No need for union, just order by the last_logged_in field descending, since you use this field to calculate if a user is logged in or not:
select * from users order by Last_logged_in desc limit 10;
This way the logged in users will be on the top of the list because they loggen in most recently.
UPDATE
Still no need for union if you want to order both lists by username, let's build on axiac's proposed solution:
SELECT u.*, IF(Last_logged_in >= UNIX_TIMESTAMP() - 60, 1, 0) as isOnline;
FROM users u
ORDER BY isOnline DESC, u.Username ASC

how to get position rank of specific row using just mysql query?

I have table Users which contains columns: id, name, points, extra_points.
How can I get position rank for specific user, when the rank is sum of points and extra_points?
For all users list rank Im using this query: "SELECT id, (points + extra_points) AS total FROM users ORDER BY total desc"; and the while loop with html ol tag (so there is position number).
But I dont know how to show the rank for a sinlge user (in user profile page)?
Any help will be great.
SELECT users1.id, (users1.points+users1.extra_points) AS total, COUNT(*)+1 AS rank
FROM users users1
INNER JOIN users users2 ON users1.points+users1.extra_points < users2.points+users2.extra_points
WHERE users1.id = $id
Well, for a single user, you'd change the select query so it looks like
"SELECT (points + extra_points) AS total FROM users WHERE id = $id LIMIT 1"
Need to specify which user as a WHERE clause. Also, since you presumably know the id already, don't need to select it, and since it's just one user, don't need the ORDER BY either. And LIMIT 1 will stop it from checking the rest of the rows once it's found it.
this isn't tested, but i hope you can understand whats my idea - simply use a subselect to count all users with more points:
SELECT
id,
(points + extra_points) AS total,
(SELECT COUNT(*) FROM users WHERE (points + extra_points)) >= (u.points + u.extra_points) AS rank
FROM
users u
WHERE
id = ?;
Here's the answer if you have a ClientID and Points column in a table called Client:
<CFQUERY DATASOURCE="YourDatasource" name="YourQueryName">
SELECT ClientID, (Pts) AS total,
(SELECT COUNT(*) FROM Client
WHERE (Pts) >= (u.Pts)) AS rank
FROM Client u
WHERE ClientID = CLIENTID_OF_YOUR_CHOICE_HERE;
</CFQUERY>

Find top contributers from DB

Each post that gets created on my site get stored in a database table, inside that table is a column which lists the users username.
I'd like to find the top 10 contributers to my site, how can I count all the posts create by all users and then display the top 10 contributers in a list.
Table name: posts
Table column: username
Each post has a username entry.
SELECT count(username) a,username from posts group by username order by a desc limit 10
Why not have a post count field? I assume this is some sort of forum, and users like to know their post count. Then you just select the top posters.
Then run something like...
SELECT username FROM tablename ORDER BY postcount DESC limit 10

Categories