English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Node.js MySQL ORDER BY is used in conjunction with SELECT FROM Query to sort records in ascending or descending order relative to the column.
By default, Node.js MySQL ORDER BY causes elements to be sorted in ascending order. For descending order of records, the DESC keyword should be used.
Example of ascending order records in the ORDER column for NUMERIC data type
Example of ascending order records in the ORDER column for TEXT data type
Example of sorting records in descending order
// Import the mysql module var mysql = require('mysql'); // Create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // The IP address of the server running mysql user: "arjun", // The username for the mysql database password: "password", // The corresponding password database: "studentsDB" // Use the specified database ); // Establish a connection with the database. con.connect(function(err) { if (err) throw err; // If the connection is successful con.query("SELECT * FROM students ORDER BY marks", function(err, result, fields) { // If any errors occur during the execution of the above query, then throw an error if (err) throw err; // If there are no errors, you will get the result console.log(result); ); );
Run the above Node.js MySQL ORDER BY example program.
AscOrderExample1.js
// Import the mysql module var mysql = require('mysql'); // Create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // The IP address of the server running mysql user: "arjun", // The username for the mysql database password: "password", // The corresponding password database: "studentsDB" // Use the specified database ); // Establish a connection with the database. con.connect(function(err) { if (err) throw err; // If the connection is successful con.query("SELECT * FROM students ORDER BY name", function(err, result, fields) { // If any errors occur during the execution of the above query, then throw an error if (err) throw err; // If there are no errors, you will get the result console.log(result); ); );
Run the above Node.js MySQL ORDER BY example program.
DescOrderExample.js
// Import the mysql module var mysql = require('mysql'); // Create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // The IP address of the server running mysql user: "arjun", // The username for the mysql database password: "password", // The corresponding password database: "studentsDB" // Use the specified database ); // Establish a connection with the database. con.connect(function(err) { if (err) throw err; // If the connection is successful con.query("SELECT * FROM students ORDER BY name DESC", function(err, result, fields) { // If any errors occur during the execution of the above query, then throw an error if (err) throw err; // If there are no errors, you will get the result console.log(result); ); );
Run the above Node.js MySQL ORDER BY example program.
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node DescOrderExample.js [ RowDataPacket {name: 'Sukumar', rollno: 11, marks: 99 }, RowDataPacket {name: 'Sai', rollno: 6, marks: 84 }, RowDataPacket {name: 'Ross', rollno: 7, marks: 54 }, RowDataPacket {name: 'Raja', rollno: 5, marks: 94 }, RowDataPacket {name: 'Prasanth', rollno: 3, marks: 77 }, RowDataPacket {name: 'Monica Gellar', rollno: 8, marks: 86 }, RowDataPacket {name: 'Lee', rollno: 9, marks: 98 }, RowDataPacket {name: 'John', rollno: 1, marks: 74 }, RowDataPacket {name: 'Bruce Wane', rollno: 10, marks: 92 }, RowDataPacket {name: 'Arjun', rollno: 2, marks: 74 }, RowDataPacket {name: 'Adarsh', rollno: 4, marks: 78 }]
records relative to name Columns are sorted in descending order.
In this Node.js tutorial – Node.js MySQL Module-In this Node.js tutorial – Node.js MySQL ORDER BY, we learned how to sort records in ascending or descending order using the example Node.js MySQL program.