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 [email protected] 1950
2 John Carter [email protected] 2400
3 Peter Parker [email protected] 3125
4 Johny Alson [email protected] 2460
5 Pierre Alexander [email protected] 3000
6 Nick Boss [email protected] 2430
7 Json Nei [email protected] 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 [email protected] 1950
2 John Carter [email protected] 2400
3 Peter Parker [email protected] 3125
4 Johny Alson [email protected] 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 [email protected] 2400
3 Peter Parker [email protected] 3125
4 Johny Alson [email protected] 2460
5 Pierre Alexander [email protected] 3000
6 Nick Boss [email protected] 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.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...