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
Related
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
I`m working on a small website where people can follow each other. There are newsfeed on the site where users can see updates from other users that they follow. The problem is that I can see only posts from other users and not my posts. I want the current user to be able to see his posts too Ordered within the other users posts. Something like Facebook Wall.
I have tried tons of differend queries and php, but I get only other users post or when i manage to pull out my posts too there are dublicate post for every user that follows me.
I have 3 tables 'members', 'Post' and 'follow'. The members table hold UserID, Name and Last Name. Post table holds the UserID(FromID in the table) Indexed to members UserID and the post from the user. In the follow table there are 3 columns FollowID, Followed and Follower both Followed and Follower are index to members UserID.
At the moment I use that query:
$query= "SELECT Post.*, members.*, follow.* FROM Post
INNER JOIN members ON Post.FromID=members.UserID
LEFT JOIN follow ON members.UserID=follow.Followed
WHERE Post.FromID='$user' OR follow.FollowerID='$user'
ORDER BY Post.date DESC, Post.time DESC";
$user is the UserID of the current logged in user.
That query returns all the posts that I want but the problem is that my post are show /n times for each user that follows me.
I really would appreciate the kindness to the one that give me some directions what i do wrong. Thank you
The problem is with your $user. Currently what is happening is that you have an OR condition in where clause. And you are trying to pull either your data or ur followers data.
My solution for you would be to remove $user from the query you have. So you will have list of followers data only. Have a different query for your posts and then while displaying sort them by datetime Timestamp so you have your posts with other followers too.
To have your posts and folloeers posts in one query.
Use union. Like :
(Query of your posts ORDER BY date)
UNION
(Query of followers posts ORDER BY date);
Also check UNION usage in w3schools.
Hope this helps
I think this query might be easier to understand:
$query = "SELECT * FROM Post,members,follow
WHERE
Post.FromID=members.UserID
AND
members.UserID=follow.Followed
AND ( Post.FromID='$user' OR follow.FollowerID='$user')
ORDER BY Post.date DESC, Post.time DESC";
I have a article pages, that i only want to show feed of the users that every account is following, kinda like twitter.
I have to distinct tables one is called posts other is called followers
I want to loop the articles but i want it to select only the users that my account is following.
I just need the SQL query that can do that since the PHP part of the rest i can perform it
Something like this , although i know that this one does not work
$connect->query("SELECT id,userid,postagem FROM posts WHERE IN (SELECT followed FROM followers WHERE whofollowed = '$userd' ) ORDER BY id DESC LIMIT 13");
Assuming followed is also a userid in the followers table:
$connect->query("SELECT id,userid,postagem FROM posts WHERE userid IN (SELECT followed FROM followers WHERE whofollowed = '$userd' ) ORDER BY id DESC LIMIT 13")
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
Okay, so I want to have a news feed on my website. I have 3 tables named Users, Follow, and Posts. Basic user data goes into the Users table, who is following who is stored in the Follow table, and whatever the user posts goes into Posts. Now, I know how to select every post from a database table and limit it using the WHERE clause. But how would I write my query to select all all of the posts from only user's they are following, and display them by DESC date? Thanks.
Here's a general layout for you, make three tables like you mentioned, I've outlined below just as an example
[TABLES]
users
followers
posts
In the users table you should have at least columns like userid (auto incremented value / primary key).
The followers table should have something like userid which would match to the users table userid, a followid column which would also have the id # for the follower from the users table.
Then for your posts table you would want to have userid too so when each post is made it would have the id from users table.
Then you would need to do something like:
SELECT p.*
FROM posts AS p
WHERE p.userid IN (SELECT followid FROM followers WHERE userid = ###)
ORDER BY p.date DESC
Now it really depends on how you are getting the users id to figure this out. If your passing the users id using a session after they logged in similar to something like Facebook, then you could change userid = ### to something like userid = ".$_SESSION['userid']." But again it really depends on how you pass the users id but the above should at least get you somewhat started.
Make sure to put indexes on the userid, followid columns so that when the table becomes larger it will do the joins quickly.
An alternative to #Shane's answer is to use the JOIN operator:
'SELECT p.* // I prefer to name all the fields, but for brevity's sake I'll use the shorthand
FROM Posts AS p
INNER JOIN Follow AS f ON p.userid = f.followid
WHERE f.userid = '.$selectedUserID.'
ORDER BY p.date DESC;'
For an inputed User ID ($selectedUserID), it will find all User ID's of the people they follow (matching follow ID to user ID on the Follow x-ref table) and then find their respective posts from the Post table in descending order by date. It will return empty if they do not follow anyone or the people they follow have no posts.
I also hope I do not need to remind you to sanitize your input to the database and escape your output to the web.
Is this what you're looking for?