Save the rank in the DB - php

I'm using this query to classify pages for which users vote for :
SELECT p.page_ID , h.point
FROM pages p
INNER JOIN history h ON h.page_ID=p.page_ID
ORDER BY h.point DESC
So I know how to display my pages ranking, but I'd like to save the rank of each page in my table. How do ?

Assuming you have in PHP $rank as the rank value and $pageid as the page_ID you wish to update (and assuming they're both integers not requiring quotes):
UPDATE history SET point=$rank WHERE page_ID=$pageid;
Or if the page does not already exist in the history table:
INSERT INTO history (page_ID, point) VALUES ($pageid, $rank);

Related

Getting Total Votes Form mysql table and Compare it?

I have a table with
id primary key, nominee_id, cat_id, user_id(voter id), vote_status, date
these fields, We have nominees for this voting process under different categories, so each nominees can be nimnated to more than one category. ok, all process is going well, I can take total count of votes got for each nominees in each category. but I dont have any idea How to get the winner from this table using SQL.
I want to get Most votes gained Nominees in each category, however as I said I can get total votes got for each nominees in each category using
SELECT nominee_id FROM voting WHEREcat_id= $cid.
Is it possible get this through another SQL statement, or else can anyone suggest any other way to get this.
below is the table, I want to get back the Nominee_id who got max Vote in a particulat cat_id, eg: in hte below table I want to get nominee_id 29 as a winner in cat_id 3, because he got two votes in that category
This query will give you the winner from each category.
SELECT nid, cid, max(votes) as final_votes from (select nominee_id as nid, cat_id as cid, count(user_id) as votes from voting group by cat_id, nominee_id) nv GROUP BY cid order by final_votes desc;
This one should work
SELECT nominee_id, COUNT(nominee_id) AS countNominee FROM voting WHERE
vote_status = 'yes' GROUP BY cat_id ORDER BY countNominee DESC

Creating a simple leaderboard without a score field. PHP/Mysql

How would one create a basic leader board ranking each user by quantity?
E.g I am trying to create a leaderboard where it ranks users by who has done the most book reviews
I have tried using the aggregate function but cant seem to figure out how to organise it into a leaderboard. Looking at other tutorials they all seem to have a score field in place to do this.
Any ideas?
SELECT
user_id,
(SELECT COUNT(*) FROM `reviews` r WHERE r.user_id = u.user_id) as user_reviews
FROM `user` u
ORDER BY
(SELECT COUNT(*) FROM `reviews` r WHERE r.user_id = u.user_id) DESC
Without knowing more details about how you store data etc, the above will list users in order of how many reviews are present in the database.
A simple way would be to make a table in MySQL, then input all the data. Then on the PHP side, when the data would need to be displayed, grab that data from the table and sort it by highest score, and descending. Then present it neatly through HTML and CSS.

Mysql table join, but return values if first table has information, but second doesnt

I have the following mysql query
SELECT Name, Summoner_Name, ROUND(SUM(timePlayed)/60) as Total_Time
FROM UserNames, games_database
WHERE (UserNames.ID = games_database.UserNames_ID AND
UserNames.ID IN ({$Member_Ids_Sql}))
GROUP BY UserNames.ID
ORDER BY Total_Time DESC;
It effectively will grab the players name, summoner names, and total play_time. It does this by using a table join. As one table Usernames, contains the users ID, Name and Summoner Name. Where as the Games_database table holds every game the player has played.
What I want to do is display the information of users that are in the UserNames table, but haven't played any games yet.
Extra Information:
UserNames Database contains
ID, Summoner_ID, Summoner_Name, Name
Games_database Database contains
ID, Match_ID, my_Date, timePlayed, champion, win, Summoner_ID, UserNames_ID, Game_Type
I got this working perfectly for all users with games, but when a new user enters the system, they aren't shown in this query due to no games being played.
You want a left join to find non-matches:
SELECT Name, Summoner_Name, ROUND(SUM(timePlayed)/60) as Total_Time
FROM UserNames LEFT JOIN
games_database
ON UserNames.ID = games_database.UserNames_ID
WHERE UserNames.ID IN ({$Member_Ids_Sql}) AND
games_database.UserNames_ID is null
GROUP BY UserNames.ID
ORDER BY Total_Time DESC;
Note: you should learn to always use explicit join syntax.
What you need here is a left outer join. A left outer join will return all results in the first table (filleted by your where) with the results in the second table. If there are no matches a null will be shown.

mysql pagination to display a page with a specific row in it

Now I have a question on mysql paging.
A user's record is displayed on a table with many other user's record. and the table is sorted/paged.
Now i need to display the page that containing the user's row directly after the user login. How can I achieve this?
A simple thought would be firstly find out the rownum of the user, then do the paging accordingly, but I'm wondering if there are better ways to do it.
thanks.
Sample table for discussion
create table t_users (id int auto_increment primary key, username varchar(100));
insert t_users(username) values
('jim'),('bob'),('john'),('tim'),('tom'),
('mary'),('elise'),('karl'),('karla'),('bob'),
('jack'),('jacky'),('jon'),('tobias'),('peter');
The query to show the page on which the user is on, not specifically putting the user to the top of the page (which would have been a lot easier)
select username, id
from
(select count(*) pos
from t_users
where username <= 'tobias') pos,
(select #row:=#row+1 row, u.*
from (select #row:=0) initvars, t_users u
order by u.username, u.id) numbered
where floor((numbered.row + 3)/4) = floor((pos.pos+3)/4);
Notes:
The page size here is 4, the number 3 is the result of taking 1 off the page size
The username of the user who just logged in is 'tobias', which is used in the first sub-query
There had better be an index along the ORDER BY clause (in this case on username)
This will cause a table scan to fully row-number the last subquery
select count(*)
FROM table
WHERE [sort_by_field] < [actual_records_value]

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>

Categories