How to get top 20 rows? - mysql

I have this database setup on records table
i want to PDO for the last 20 by the reference, this is an auto increment number by php but i need an sql statement that would pull out the top 20 meaning the recent 20 records
so far..
SELECT TOP 20
FROM records
ORDER BY reference DESC

Use LIMIT <offset>, <recordcount>
SELECT * FROM records
ORDER BY reference DESC
LIMIT 0, 20

Make use of the LIMIT Keyword
$display_query = $dbh->prepare("SELECT *
FROM records
ORDER BY reference DESC LIMIT 20");
$display_query->execute();
echo $display_query->fetchAll();
TOP keyword is used in ORACLE , LIMIT should be used on MySQL

Related

SQ L - how to select the next latest 10 records from a specified position in a table?

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

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;

LIMIT does not work correctly

I have a problem when I need range from 10 to 20 (LIMIT 10, 20) it is returns everything from 1 to 20. I don't have any idea why it's works so.
Here is a query:
SELECT *
FROM table
LIMIT 10, 20
This table has 5 foreign keys, can it be a reason?
The limit syntax is LIMIT offset, rowcount. So you're asking for 20 rows, starting with the 10th. You probably want LIMIT 10, 10.
The limit command works as follows:
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
Your query should be:
SELECT *
FROM table
LIMIT 10,10
the first number indicates which record to start from, and the second indicates the amount of records to limit to.
You should be able to get what you want with limit 10 offset 10

limit after group of the result 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.

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.