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

Node.js MySQL Order By


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

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:

SELECT FROM table_name(s) ORDER BY column_name(s) ASC|DESC

Once installed this Node.js module can be used to manipulate the MySQL database:

var mysql = require('mysql');

 

ORDER BY to sort the result by default

Select records from “Users” Table and sort the result alphabetically by name.

Example: Sort the result alphabetically by name::
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;
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:

E:\Users\Your_Name>node demo_test_db_sort.js
Note: "SELECT * FROM Users" will return all rows with the same address from Users table

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.

Example: ORDER BY DESC keyword to sort the result:
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;
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:

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

 

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

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.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
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()...