I have a game that a user can save there name and score in a database.
Columns
Name, Tries, Percentage correct, Time taken, Image set
3 of these columns are based on a score
Tries, Percentage correct, Time taken,
Currently I have the table display time ascending.
$sql = "SELECT * FROM Score ORDER BY time ASC LIMIT 5";
Is there away to compare the scores
This is an example of 2 scores
1st row: 46, 52%, 02:36
2nd row: 38, 63%, 02:47
Is there away that i can compare on average which of those should be top based on all 3 scores.
Row one had more tries and less percentage correct but faster time.
Row two had less tries and higher percentage correct but slower time.
In a theory tries: ASC, percent: DESC, time Asc
If I change the Order by to:
$sql = "SELECT * FROM Score ORDER BY 'time ASC', 'tries ASC', 'percent
DESC' LIMIT 5";
Will it mess the rows up or will it display them in an order based on all 3
This image is using time ASC
The minimum amount of tries is 12 which will be 100%
Some how I need to compare Tries with Time
Can I divide time / tries and then order by result
Is the right?
$sql = "SELECT * FROM Score ORDER BY time / tries ASC LIMIT 5";
If you want to divide 2 columns then order that result use
$sql = "SELECT * FROM Score ORDER BY time / tries ASC LIMIT 18446744073709551615";
Then if you wish you can display that result in your table
For example
<td>".round($data[4] / $data[2],2)." Seconds</td>
/ will divide time by tries and then wrap with round() to round down the result.
In this case I would recommend round(??? ,2) which will output something like 3.53 Seconds
If you want an average score then use time + tries - percent
Lowset possible tries = 12
Time could be still high even with 12 tries
getting 12 tries will give 100% that is a high value so its best to minus that.
12 tries + 1m 30s - 100%
12 tries + 10h 30m 30s - 100%
This is too long for a comment.
"Is there away that i can compare on average which of those should be top based on all 3 scores?"
Yes. But first you have to figure out what the method is. Then you can implement it in a query.
Second, your query as written is:
SELECT *
FROM Score
ORDER BY 'time ASC', 'tries ASC', 'percent DESC'
LIMIT 5;
This will do nothing, because it is ordering by three constants. Drop the single quotes, and only use them for string constants:
SELECT *
FROM Score
ORDER BY time ASC, tries ASC, percent DESC
LIMIT 5;
In practice, this would be very much like:
ORDER BY time ASC
unless a lot of people have exactly the same time on two rows, the additional ordering criteria will not be used.
Related
I have a table of pdf titles in a MYSQL database and I am trying to load these dynamically onto my website only 10 at a time as the user scrolls down to the bottom of the page. I am ok with the ajax request but I am stuck on the SQL statement required to capture the next 10 titles from a specified position in the table.
Let us say the last loaded title had an ID of 10.
My SQL attempt:
SELECT titles FROM pdfLibrary ************ ORDER BY DATE ASC LIMIT 10
I would like some help finishing this SQL statement.
You can use LIMIT with an OFFSET, e.g.
SELECT titles
FROM pdfLibrary
ORDER BY DATE ASC
LIMIT 10 OFFSET 10
Programatically, you would replace OFFSET 10 with OFFSET ID, assuming you wanted to use the ID to keep track of where you were in your count.
You can use 2 parameters with the Limit for example if you want to get result 10 to 20
SELECT titles FROM pdfLibrary ORDER BY DATE ASC LIMIT 10,20;
this will return you to records of 10 to 20
I have a query that I use to determine what the interval is between timestamps of gauge data using the 2 most recent readings:
$interval_query = sprintf("SELECT `stamp`
FROM `processed_gauge_data`
WHERE `processed_gauge_data`.`gauge_id` IN (%s)
ORDER BY `processed_gauge_data`.`stamp` DESC LIMIT 2;",
$gauge_id
);
Here is an image with EXPLAIN results as well as the structure of the table:
http://i.imgur.com/QJmHmeb.png?1
This has worked fine for most gauges, but there are 2 in particular that it takes 30-45 seconds to execute this query. Selecting all data for those 2 gauges takes less than a second. What is causing this? I don't understand what's going on.
Turns out it was because of ORDER BY processed_gauge_data.stamp DESC. I changed my query to ORDER BY 'id' and it went from 30-45 seconds to .0006-.0003 seconds:
$interval_query = sprintf("SELECT `stamp`, 'id'
FROM `processed_gauge_data`
WHERE `processed_gauge_data`.`gauge_id` IN (%s)
ORDER BY `processed_gauge_data`.`id` DESC LIMIT 2;",
$gauge_id
);
I want to make a sqlite query in such a way that the result should be sorted which has a LIMIT and the OFFSET. But the OFFSET should work in synch a manner that it should discard the last records from the result.
SELECT * FROM TempTable WHERE CLASS = 1 ORDER BY Date ASC LIMIT 100 OFFSET 5;
The above query just ignores the first 5 records from the table and give the remaining records. But instead I want it to ignore the first 5 latest entries.
Note:- the first 5 latest entries means since I am sorting it by date it should IGNORE the latest record inserted in the table respecting the date.
Sort backwards, with OFFSET 5 and resort again:
SELECT * FROM (
SELECT * FROM TempTable WHERE CLASS = 1 ORDER BY Date DESC LIMIT 100 OFFSET 5
) ORDER BY Date ASC;
I have following the mysql query
SELECT * FROM tbltest WHERE DATE(posted_date) BETWEEN '20120414' AND '20130414' GROUP BY title ORDER BY mostviewed DESC LIMIT 30
Problem:
It return only 19 rows where row of duplicate title is eliminated but I want to return 30 rows after grouping. How can I do this?
LIMIT 30 - limits the result to a maximum of 30, but if you have less results according tot he filter you are using you get as much results as your query finds as long as less than 30.
If you wish more than 30 results you will need to change your filter, but only to get more results is non usual reason for changing a filter.
I am trying to do a mysql query to select some news stories from a table, now the key is I always need 5 results.
so I was hoping to be able to pad out my results with another where clause ie
select * from here where this = 1
if there is < 5 results then select * from here where this = 2
limit [how ever many we are short say the first query brings back 3 results this should bring back 2]
Now I've looked at using a union to do this but without outside help ie from php and another query to count the results I don't think it is possible, I know I could simply use php to do this, and will probably end up doing that, but I was just wondering if what I am trying to do is possible with one mysql query?
EDIT:
also it needs to order by date but they are not really posted in order so
order by date get upto 5 where this = 1 and if there isn't 5 pad it out with the remainder of where this = 2 also ordered by date.
Another Shameful Edit:
ask a silly question lol... it was my sleep deprivation I just assumed there was data in the table and the previous coder was using unions to do all sorts of stuff, making me think it was more complex than it should be
SELECT *
FROM
news
WHERE
( this = 45 || this= 0 )
AND
active = '1'
ORDER BY
this ASC,
date_added DESC
LIMIT 5
How about -
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5;
Or are you after the five results then being sorted by date again -
SELECT *
FROM (
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5
) AS tmp
ORDER BY `date` ASC
You could combine the where clauses and use limit :
select * FROM here WHERE this = 1 OR this = 2 ORDER BY this LIMIT 5
Even in there were 15 records where this is equal to 1 this would only bring back 5 records ...