bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Node.js/Database Integration
Node.js•Database Integration

Node.js MySQL Limit

Limit the Result

You can limit the number of records returned from the query, by using the "LIMIT" statement:

Example

Select the 5 first records in the "customers" table:

let mysql = require('mysql');
let con = mysql.createConnection({
 host: "localhost", user: "yourusername",
 password: "yourpassword", database: "mydb"
});
con.connect(function(err) {
 if (err) throw err;
 let sql = "SELECT * FROM customers LIMIT 5 ";
 con.query(sql, function (err, result) {
 if (err) throw err;
 console.log(result);
 });
});

Save the code above in a file called "demo_db_limit.js" and run the file:

Run "demo_db_limit.js"

C:\Users\
Your Name
>node demo_db_limit.js

Which will give you this result

[ { id: 1, name: 'John', address: 'Highway 71'}, { id: 2, name: 'Peter', address: 'Lowstreet 4'}, { id: 3, name: 'Amy', address: 'Apple st 652'}, { id: 4, name: 'Hannah', address: 'Mountain 21'}, { id: 5, name: 'Michael', address: 'Valley 345'}
]

Start From Another Position

If you want to return five records, starting from the third record, you can use the "OFFSET" keyword:

Example

Start from position 3, and return the next 5 records:

let mysql = require('mysql');
let con = mysql.createConnection({
 host: "localhost", user: "yourusername",
 password: "yourpassword", database: "mydb"
});
con.connect(function(err) {
 if (err) throw err;
 let sql = "SELECT * FROM customers LIMIT 5 OFFSET 2 ";
 con.query(sql, function (err, result) {
 if (err) throw err;
 console.log(result);
 });
});

Note

"OFFSET 2", means starting from the third position, not the second!

Save the code above in a file called "demo_db_offset.js" and run the file:

Run "demo_db_offset.js"

C:\Users\
Your Name
>node demo_db_offset.js

Which will give you this result

[ { id: 3, name: 'Amy', address: 'Apple st 652'}, { id: 4, name: 'Hannah', address: 'Mountain 21'}, { id: 5, name: 'Michael', address: 'Valley 345'},
 { id: 6, name: 'Sandy', address: 'Ocean blvd 2'}, { id: 7, name: 'Betty', address: 'Green Grass 1'}
]

Shorter Syntax

You can also write your SQL statement like this "LIMIT 2, 5" which returns the same as the offset example above:

Example

Start from position 3, and return the next 5 records:

let mysql = require('mysql');
let con = mysql.createConnection({
 host: "localhost", user: "yourusername",
 password: "yourpassword", database: "mydb"
});
con.connect(function(err) {
 if (err) throw err;
 let sql = "SELECT * FROM customers LIMIT 2, 5 ";
 con.query(sql, function (err, result) {
 if (err) throw err;
 console.log(result);
 });
});

Note

The numbers are reversed: "LIMIT 2, 5" is the same as "LIMIT 5 OFFSET 2"

Previous

Node.js MySQL Update

Next

Node.js MySQL Join

This chapter

Overview
24

Lessons

70m

Read time

1. Node.js MySQL2. Node.js MySQL Create Database3. Node.js MySQL Create Table4. Node.js MySQL Insert Into5. Node.js MySQL Select From6. Node.js MySQL Where7. Node.js MySQL Order By8. Node.js MySQL Delete9. Node.js MySQL Drop Table10. Node.js MySQL Update11. Node.js MySQL Limit12. Node.js MySQL Join13. Node.js MongoDB14. Node.js MongoDB Create Database15. Node.js MongoDB Create Collection16. Node.js MongoDB Insert17. Node.js MongoDB Find18. Node.js MongoDB Query19. Node.js MongoDB Sort20. Node.js MongoDB Delete21. Node.js MongoDB Drop22. Node.js MongoDB Update23. Node.js MongoDB Limit24. Node.js MongoDB Join

On this page

Limit the ResultStart From Another PositionShorter Syntax