Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Mysql Limit in NodeJs

Mysql Limit in NodeJs

In MySQL with Node.js, the LIMIT clause is used to restrict the number of records returned in a query β€” super useful for pagination or performance optimization.

Let’s walk through how to use LIMIT in your Node.js app πŸ‘‡


βœ… Step 1: Install MySQL package

bash

npm install mysql const mysql = require('mysql');const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase'});connection.connect();const sql = 'SELECT * FROM users LIMIT 3';connection.query(sql, (err, results) => { const page = 2;const limit = 3;const offset = (page - 1) * limit;const sql = 'SELECT * FROM users LIMIT ? OFFSET ?';connection.query(sql, [limit, offset], (err, results) => { if (err) throw err; console.table(results);});


πŸ”Ή Using mysql2 (async/await)

js

const mysql = require('mysql2/promise');async function fetchLimitedUsers() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase' }); const [rows] = await connection.execute('SELECT * FROM users LIMIT ?', [3]); console.table(rows); await connection.end();}fetchLimitedUsers();


🧠 Bonus: Sort + Limit

sql

SELECT * FROM users ORDER BY age DESC LIMIT 2;

This returns the top 2 oldest users.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql