1

I have a database name BOOKS and I want to retrieve only a limited no. of books from the database everytime I query it. Say, 140 books only, even if the result of the query is more than 140 records.

1 Answers1

0

To limit data selections from a MySQL database use the LIMIT clause. It is used to specify the number of records to retrieve. It also helps improve performance by not retrieving all the records in the database.

If you wish to select all records from 1 - 140 (inclusive) from a table called "BOOKS". The SQL query would then look like this:

$sql = "SELECT * FROM BOOKS LIMIT 140";

When the SQL query above is run, it will return the first 30 records.

The SQL query below says "return only 100 records, start on record 140 (OFFSET 139)":

$sql = "SELECT * FROM BOOKS LIMIT 100 OFFSET 139";

Alternatively you could use this syntax to get the same result:

$sql = "SELECT * FROM BOOKS LIMIT 139, 100";

Prajwal
  • 246
  • 1
  • 5
  • 16