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

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

Related

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

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

Using an array in an SQL query

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?

MySql conditional SQL. Is my scenario possible?

Let me set up the situation first.
I have a "users" table with X fields, the fields dont really matter for my question except for "visibility". Visibility is a tinyint and the values mean the following (0 = visible to all, 1 = visible to friends only, and 2 = invisible).
I also have a friends table (id, user_id, target_user_id). user_id is friends with target_user_id. Easy enough so far right?
Here is where it gets sticky. Im writing a PHP API and my class method looks kinda like this:
public function getUsers($requester, $page, $num) {}
the $requester is the user id of the person requesting the users
the $page is the pagination page number
the $num is the number of items per page
What I want to do in SQL is get $num users from the users table if their visibility field is = 0 or 1. If the visibility flag is 1 however, I need to make sure the user id and the $requester are friends in the friends table and only return that user if they are friends.
I thought about using PHP to filter the visibility after I get my results back but the pagination (limit) will be screwed up if I ask for 5 records for example and one or more user has visibility set to 1 and are not friends with the requester. This pretty much has to be done entirely thru sql.
Possible??
Try creating a temp table 'temp' with same structure as users.
select * into temp From users where visibility=0 or visibility=1;
Select * from temp, friends where (temp.visibility=0) or (temp.user_id = friends.target_user_id);
Don't forget to empty the temp table.
I haven't tried the second query yest, let me know what output you got.
select * from users, friends where (users.visibility=0) or (users.visibility=1 and users.user_id = friends.target_user_id);
I think you can use LEFT JOIN for this.. something like
"SELECT *
FROM users t1
LEFT JOIN friends t2 ON t2.user_id=t1.id AND t1.visibility=1
INNER JOIN user t3 ON t3.id=t2.target_user_id
WHERE t1.visibility=0 OR t1.visibility=1
LIMIT ".($page*$num).",".$num

PHP: Search in one table and in another

I am making an autosuggesting function, when the user writes something in the field it stores it in:
$queryString = $db->real_escape_string($_POST['queryString']);
I want it to autosuggest after the users friends. The user´s friends is in users_friends, but only the friend´s ID. Now their full_name is in the table "users". And i want when you search it should in users for the full_name + check if its friends with the user.
As you may understand i do not expect all my users to know eachother id´s so writing e.g "52" "233", but searching for their full_name s.
UPDATE:
I know tried doing this:
$query = $db->query("SELECT uf.bID
FROM users friends, users_friends uf
WHERE uf.uID = '1' AND uf.type = 'friend' AND friends.full_name LIKE '$queryString%' LIMIT 10;"
);
It selects the bID, from the users friends WHERE the userid is 1 and are friend.
Now i start to see some results i think. When i write a full_name that im friends with, i get the id of the user(the id that is stored in bID). Now i just need to grab the full_name in "users" where id = bID..
table: users
id | full_name
table: users_friends
id | uID | bID
So conclusion of all this (trying to make a better summary in order to make you understand better: )
When you type in e.g Jack in the search field, then the $queryString is now "jack". Then it is taking "Jack"(full_name in users), grabbing his id(id in users), if he exists there ofcourse, and then match it with bID (in users_friends) where uID is $USER; ($user is the current user that are logged in´s id.)
Hope this was easier to understand, please leave comment if theres something unclear.
So, as i figure it out, you've got the current user's id in $USER and its query string in $queryString, and what you want is the names of the user's friends based on the $queryString, am I right?
So, assuming the database's schema is as you've put:
table: users
id | full_name
table: users_friends
id | uID | bID
See if this query works out for you, then:
SELECT users.full_name
FROM users INNER JOIN users_friends ON users.id=users_friends.uID
WHERE bID=$USER AND users.full_name LIKE '$queryString%'
LIMIT 10;
Where $USER and $queryString are your variables.
Do you want to read data from many tables at once??
SELECT table1.id, table2.name FROM table1, table2 WHERE ...
My english is not very good to understand everything :D
I didn't understood your question
But you can use this query:
mysql_query("SELECT * FROM table_one, table_two WHERE table_one.id = table_two.id");

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