AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Limit


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js MySQL Limit

"LIMIT" statement is used to set the number of records returned from the SQL Query.

Syntax:

"SELECT * FROM table_name LIMIT value"

 

Let us to have a table "Users" as:

ID Name Email 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:

Example: UPDATE records
var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
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:

E:\Users\Your_Name>node demo_test_db_limit.js

Which will give you this result after run the file your table will be:

ID Name Email 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:

Example:
var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
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:

Example: : Run "demo_test_db_offset2.js"
E:\Users\Your_Name>node demo_test_db_offset2.js

Your database will be:

ID Name Email 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
Note: "OFFSET 1", this means starting from the second position, not from the first!
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:

Example:
var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
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);
});
});
Note: "LIMIT 1, 5" is the same as "LIMIT 5 OFFSET 1"

 



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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...