Node.js MySQL Limit
Node.js Tutorial » Node.js MySQL Limit
"LIMIT" statement is used to set the number of records returned from the SQL Query.
Syntax:
Let us to have a table "Users" as:
ID | Name | wage | |
---|---|---|---|
1 | Clark K | clarkkent@mail.com | 1950 |
2 | John Carter | johncarter@mail.com | 2400 |
3 | Peter Parker | clarkkent@mail.com | 3125 |
4 | Johny Alson | johnyAlson@mail.com | 2460 |
5 | Pierre Alexander | pierrealexander@mail.com | 3000 |
6 | Nick Boss | NickBoss@mail.com | 2430 |
7 | Json Nei | Json-Nei@mail.com | 2900 |
Node js MySQL Limit Clause
We select 4 first rows from "Users" table:
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
var sql = "SELECT * FROM Users LIMIT 4";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file named "demo_test_db_limit.js", and run the file:
Which will give you this result after run the file your table will be:
ID | Name | wage | |
---|---|---|---|
1 | Clark K | clarkkent@mail.com | 1950 |
2 | John Carter | johncarter@mail.com | 2400 |
3 | Peter Parker | clarkkent@mail.com | 3125 |
4 | Johny Alson | johnyAlson@mail.com | 2460 |
Related subjects:
node.js create database
nodejs create table
node.js MyS
MySQL drop table
Tags: node js mysql connection limit
What is connection limit in node js?
What is MySQL connection pooling?
mysql limit offset
limit in mysql
query in node js
offset in mysql
MySQL limit offset
If you need to return 5 rows, starting from second record, you have to use " OFFSET " keyword:
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
var sql = "SELECT * FROM Users LIMIT 5 OFFSET 1";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file called "demo_test_db_offset2.js", and run the file:
Your database will be:
ID | Name | wage | |
---|---|---|---|
2 | John Carter | johncarter@mail.com | 2400 |
3 | Peter Parker | clarkkent@mail.com | 3125 |
4 | Johny Alson | johnyAlson@mail.com | 2460 |
5 | Pierre Alexander | pierrealexander@mail.com | 3000 |
6 | Nick Boss | NickBoss@mail.com | 2430 |
If you have "OFFSET 25" this mean starting from 26 row.
OFFSET Shorter Syntax
For Shorter OFFSET Syntax you can have something like this "LIMIT 1, 5" which returns the same as above example OFFSET:
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
var sql = "SELECT * FROM Users LIMIT 1, 5";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
node js mysql connection limit
What is connection limit in node js?
What is MySQL connection pooling?
mysql limit offset
limit in mysql
query in node js
offset in mysql
Node.js MySQL Limit - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.