English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS 基础教程

NodeJS Express.js

NodeJS 缓冲&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.js-MySQL-ORDER BY

Node.js MySQL ORDER BY用于与SELECT FROM Query结合使用以相对于列按升序或降序对记录进行排序。

Node.js-MySQL-ORDER BY

默认情况下,Node.js MySQL ORDER BY导致元素的升序。对于记录的降序,应使用DESC关键字。

  • NUMERIC数据类型的升序wrt列中的ORDER记录示例

  • TEXT数据类型的升序wrt列中的ORDER记录示例

  • 以降序排列记录的示例

NUMERIC数据类型的升序wrt列中的ORDER记录示例

// 引入mysql模块
var mysql = require('mysql'); 
 
// 创建具有所需详细信息的连接变量
var con = mysql.createConnection({ 
  host: "localhost", // 运行mysql的服务器的IP地址
  user: "arjun", // mysql数据库的用户名
  password: "password", // 对应的密码
  database: "studentsDB" // 使用指定的数据库
 }); 
 
// 建立与数据库的连接。
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 while executing the above query, 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

// 引入mysql模块
var mysql = require('mysql'); 
 
// 创建具有所需详细信息的连接变量
var con = mysql.createConnection({ 
  host: "localhost", // 运行mysql的服务器的IP地址
  user: "arjun", // mysql数据库的用户名
  password: "password", // 对应的密码
  database: "studentsDB" // 使用指定的数据库
 }); 
 
// 建立与数据库的连接。
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 while executing the above query, 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

// 引入mysql模块
var mysql = require('mysql'); 
 
// 创建具有所需详细信息的连接变量
var con = mysql.createConnection({ 
  host: "localhost", // 运行mysql的服务器的IP地址
  user: "arjun", // mysql数据库的用户名
  password: "password", // 对应的密码
  database: "studentsDB" // 使用指定的数据库
 }); 
 
// 建立与数据库的连接。
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 while executing the above query, 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.

Conclusion:

In this Node.js tutorial – Node.js MySQL Module-In this Node.js tutorial – Node.js MySQL Module, we learned how to sort records in ascending or descending order using the Node.js MySQL example program.