MySQLi : How to sort to make a ladder - php

I'm setting up a website based on server voting's. I would like to sort servers votes with mysqli and give them a rank, per example:
1: 88 votes Server1
2: 54 votes Server2
3:34 votes Server3...
I already have my servers and votes in my database, but I can't find the solution to sort them and to give them a rank.
It is like:
{rank} : {number of server's vote} {server name}
I already tried this:
$totalserv = $database->prepare("SELECT COUNT(*) FROM `servers` ");
$votes = $database->query("SELECT `votes` FROM `servers` ORDER BY votes ASC");
But I don't understand how to make $vote a list and attribute it a rank.

You can try this, change DESC and ASC accordingly
SELECT * FROM `table` ORDER BY LEFT(`votes`,2) DESC;
Your query
$votes = $database->query("SELECT `votes` FROM `servers` ORDER BY LEFT(`votes`,2) ASC");

Related

Mysql ASC function ORDER only first ID's

I have this line:
$query = mysql_query("SELECT * FROM livechat WHERE type='public' ORDER BY id ASC LIMIT 15") ;
And this is for chat, however ASC takes only first ID comments, so it shows only 15 old comments (id1, id2 and so on). If I use DESC instead of ASC, it shows new comments, but in a bad way - newest at the top, since this is a chat, newest comments must be at the bottom.
Try creating a temporary table that contains the last 15 results, and then ordering from that table.
select * from (
select * from livechat where type='public' order by id desc limit 15
) tmp order by tmp.id asc
try like this:
$query = mysql_query("SELECT *
FROM (
SELECT *
FROM livechat
WHERE type='public'
ORDER BY id DESC LIMIT 15
) t
order by t.id") ;

Get total points of user in MySQL Table with PHP

I have a MySQL table with the columns "user" and "warningpoints" for each warning as you can see in the table above. How can I get the user which has the most warningpoints in total in PHP?
You can use GROUP BY and ORDER BY and LIMIT:
SELECT t.user,sum(t.warningPoints) as sum_points
FROM YourTable t
GROUP BY t.user
ORDER BY sum_points DESC
LIMIT 1;
Or if there is only one record per person, no need to group :
SELECT t.user,t.warningPoints
FROM YourTable t
ORDER BY t.warningPoints DESC
LIMIT 1;
You can simply add below statements in your php code.
$sql = "SELECT User, Max(warningpoints) AS MaxWarningpoints FROM MyGuests GROUP BY User";
$result = $conn->query($sql);

SQL Order By id and Count star not working

I would like to get number of all records and get last record :
$sql_count_sms = "SELECT count(*) as total,content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = $row_num_sms['total'];
$last_my_sms = $row_num_sms['content'];
I can get number of records but I can't get last content record .
It returns first record !
Where is my wrong ?
Below codes works fine, but I think count(*) is faster than mysql_num_rows .
$sql_count_sms = "SELECT content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = mysql_num_rows($result_count_sms);
$last_my_sms = $row_num_sms['content'];
Any solution?
The grain of the two results you want is not the same. Without using a sub-query you can't combine an aggregate and a single row into the same result.
Think of the grain as the base unit of the result. The use of GROUP BY and aggregate functions can influence that "grain"... one result row per row on table, or is it grouped by user_id etc... Think of an aggregate function as a form of grouping.
You could break it out into two separate statements:
SELECT count(*) as total FROM android_users_sms WHERE user_id = :id;
SELECT * FROM android_users_sms WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Also, specific to your question, you probably want a LIMIT 1 in combination with the ORDER BY to get just the last row.
Now, counter intuitively perhaps, this should also work:
SELECT count(*), content, id
FROM android_users_sms
WHERE user_id = :id
GROUP BY id, content
ORDER BY id
LIMIT 1;`
This is because we've changed the "grain" with the GROUP BY. This is the real nuance and I feel like this could probably be explained better than I am doing now.
You could also do this with a sub query like so:
SELECT aus.*,
(SELECT count(*) as total FROM android_users_sms WHERE user_id = :id) AS s1
FROM android_users_sms AS aus
WHERE user_id = :id ORDER BY id DESC LIMIT 1;

Order by: Difference between two columns

i have a question about a sql query. I have two columns in table users:
Moneytotal and Moneythisweek
Right now i would like to order the results on difference.
How bigger the difference between Moneytotal and Moneythisweek the higher on the ranking.
Normally i use:
$lsel_rank = mysql_query("select * from users ORDER BY Moneytotal DESC");
$rank = mysql_fetch_array($lsel_rank);
But now i want to do something like:
lsel_rank = mysql_query("select * from users ORDER BY Difference between Moneytotal
AND Moneythisweek DESC");
Can anyone help me?
SELECT * FROM `users` ORDER BY Moneytotal - Moneythisweek DESC
Just simply use this
$lsel_rank = mysql_query("select *,'moneytotal - monethisweek' as difference from users ORDER BY difference DESC");
Option 1: (Doing calculation in orderby)
SELECT * FROM `users` ORDER BY (Moneytotal - Moneythisweek) DESC;
Option 2: (Using numbered index in order by and calculating the difference and make available in the select query's resultset
SELECT (Moneytotal - Moneythisweek) difference, a.* FROM `users` a ORDER BY 1 DESC;

PHP / MySQL Checking data between tables with long query

This is a more detailed question as my previous attempt wasn't clear enough. I'm new to MySQL and have no idea about the best way to do certain things. I'm building a voting application for images and am having trouble with some of the finer points of MySQL
My db
_votes
id
voter_id
image_id
_images
id
file_name
entrant_id
approved
_users
id
...
Basically I need to do the following:
tally up all votes that are approved
return the top 5 with the most votes
check if the user has voted on each of these 5 (return Boolean) from another table
I've tried variations of
SELECT i.id, i.file_name, i.total_votes
FROM _images i WHERE i.approved = 1
CASE WHEN (SELECT count(*) from _votes v WHERE v.image_id = i.id AND v.voter_id = ?) > 0 THEN '1' ELSE '0' END 'hasvoted'
ORDER BY i.total_votes DESC LIMIT ".($page*5).", 5
is that something I should try and do all in one query?
This query was working fine before I tried to add in the 'hasvoted' boolean:
SELECT id, file_name, total_votes FROM _images WHERE approved = 1 ORDER BY total_votes DESC LIMIT ".($page*5).", 5
At the moment I'm also storing the vote count in the _images table and I know this is wrong, but I have no idea about how to tally the votes by image_id and then order them.
Let me give this a shot to see if I understand your question:
SELECT i.*,(SELECT COUNT(*) FROM _votes WHERE i.id = image_id) AS total_votes, (SELECT count(*) from _votes where i.id = image_id and user_id = ?) as voted FROM _images AS i WHERE i.approved = 1

Categories