Node.js MySQL Order By
Node.js Tutorial » Node.js MySQL Order By
How to use MySQL Order By in Node.js?
ORDER BY sort the result-set in ascending or descending order.
ORDER BY is used to sorts the records in ascending order by default. Sorting records in descending order, you have to use the DESC keyword.
Syntax:
Once installed this Node.js module can be used to manipulate the MySQL database:
ORDER BY to sort the result by default
Select records from “Users” Table and sort the result alphabetically by name.
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
conn.query("SELECT * FROM Users ORDER BY name", function (err, result) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file named "demo_test_db_sort.js", and run the file:
After run the file will give you this result:
[
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 2, name: 'Chris', address: 'Avenida 38'},
{ id: 6, name: 'Jack', address: 'Avenida 38'},
{ id: 4, name: 'Jony', address: 'Avenida 38'},
{ id: 1, name: 'Maria', address: 'Avenida 38'},
{ id: 5, name: 'Micha', address: 'Avenida 38'}
]
Related subjects:
node.js create database
nodejs create table
node.js MySQL select from
where clause
Tags: Node.js MySQL Order By date, value, string, key, name, byte
order by sequelize node js
js sort descending
node js sort array of objects
node js mongodb sort by date
js sort alphabetically
MySQL ORDER BY DESC
Use ORDER BY DESC keyword to sort the result in a descending order from name column in table Users.
var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
if (err) throw err;
conn.query("SELECT * FROM Users ORDER BY name DESC", function (err, result) {
if (err) throw err;
console.log(result);
});
});
Save the code above in a file called "demo_test_db_sort2.js", and run the file:
Result for MySQL ORDER BY DESC
After run the file will give you this result:
[
{ id: 5, name: 'Micha', address: 'Avenida 38'}
{ id: 1, name: 'Maria', address: 'Avenida 38'},
{ id: 4, name: 'Jony', address: 'Avenida 38'},
{ id: 6, name: 'Jack', address: 'Avenida 38'},
{ id: 2, name: 'Chris', address: 'Avenida 38'},
{ id: 3, name: 'Amy', address: 'Apple st 652'},
]
Node.js MySQL Order By date, value, string, key, name, byte
order by sequelize node js
js sort descending
node js sort array of objects
node js mongodb sort by date
js sort alphabetically
Node.js MySQL Order By - 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.