limit after group of the result mysql - mysql

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.

Related

get MAX date using limit and offset

Is there a way to use limit offset and get the most recent (MAX) date from that group
My table: column_id, column_data, column_date
I've tried
SELECT max(column_date) FROM table_name limit 2000 offset 22000
I'm trying to get the most recent date in the 2000 rows returned using the offset. In other words, I'm looking for the last date modified in each group of 2000.
The table structure above has 100,000 rows. each query gets 2000 rows and I would like to retrieve the most recent date from the 2000 rows (using offset).
You must extract the whole group then find MAX() over it:
SELECT MAX(date_column)
FROM ( SELECT date_column
FROM source_table
ORDER BY some_expression /* compulsory! must provide rows uniqueness! */
LIMIT #rows_in_group OFFSET #group_offset ) AS subquery

mysql compare 3 columns order on overall

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.

Sqlite Query select statement with sorted result respecting the OFFSET

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;

MySQL: Why do these queries return different results?

Query 1:
SELECT *
FROM user_d1
WHERE EXISTS (SELECT 1
FROM `user_d1`
WHERE birthdate BETWEEN '1989-08-04' AND '1991-08-04')
ORDER BY timestamp_lastonline DESC
LIMIT 20
Query 2:
SELECT *
FROM user_d1
WHERE birthdate BETWEEN '1989-08-04' AND '1991-08-04'
ORDER BY timestamp_lastonline DESC
LIMIT 20
And what I really don't understand: why does Query 2 return the wrong results? It returns a list ordered first by birthdate and then by timestamp_lastonline...
Query 1 : If at least one record between the dates exists then the entire talbe is retrieved.
Query 2 : Only records between the dates are retrieved.
Read here for how EXISTS works.
Your second query uses BETWEEN to return every entry BETWEEN the first entry with '1989-08-04' and the next entry with '1991-08-04' and then orders these by timestamp_lastonline DESC. Note that this is literally returning the entries between the two entries with those two values, not every entry that has a year between 1989 and 1991(unless you manually ordered these to be chronologically indexed!). I'm interested to see what you think your first query returns, as it'll get every entry in the table ordered by timestamp_lastonline if there's a row that the BETWEEN clause returns.

SQL LIMIT doesn't work as excepted

I always thought that LIMIT in a query selects only between numbers I set.
Suppose I have two columns to be selected with primary key - id. I have 90 rows.
SELECT name, lastname
FROM some_table
WHERE id = '123' LIMIT 0, 30
will select exactly 30 rows. Ok, but
SELECT name, lastname
FROM some_table
WHERE id = '123' LIMIT 30, 60
will select more than 30 rows. Well, I am doing this for AJAX paginator using PHP+mysql.
How can I do this right way?
limit is offset, rowcount. so it should be limit 30,30, not 30, 60, if you want 30 rows.
SELECT name, lastname FROM some_table WHERE id = '123' LIMIT 30, 30;
In simple language,
First 30 is the starting record number
Second 30 is the 30+30 which is 60th record.
This should work to get the records between 30 and 60.
When you specify two arguments to LIMIT, the second one is the maximum number of rows to return; so instead of LIMIT 30, 60, write LIMIT 30, 30. (See the explanation of LIMIT in ยง12.2.9 "SELECT Syntax" of the MySQL 5.6 Reference Manual.)
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
second parameter is row count.
Read the documentation.
LIMIT 30 -- get 30 records
LIMIT 30, 60 -- skip 30 records and get next 60
According to the MySQL Documentation, the second number of the limit statement is the number of rows to be returned. If you want to get rows 31-60, then you need to specify
SELECT name, lastname FROM some_table WHERE id = '123' LIMIT 30, 30
You're asking MySQL to give you 60 rows start at offset 30.