Node.js MySQL - REVERSE()

In this post, we will discuss how to reverse the values present in the column of MySQL table in XAMPP Server using Node.js application.

It is important to install mysql package in node.js.

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

REVERSE() Function:

REVERSE() is used to reverse the string values in a column in table.

Steps for Node.js script:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses REVERSE() function
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT REVERSE(column),.... 
      FROM table_name WHERE condition/s...", function (error, result) {
         console.log(result);
      });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the village2 table with the following records:

alt=

REVERSE Example 1:-

Let's return the reversed values from name column using Node.js script

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {  
  // Write sql query to return actual values, 
  //reverse values from the name column.
  connection_data.query("SELECT name, REVERSE(name) 
  FROM village2;", function (error, result) {
 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket { name: 'pnp', 'REVERSE(name)': 'pnp' },
  RowDataPacket { name: 'pnp', 'REVERSE(name)': 'pnp' },
  RowDataPacket { name: 'bkpalem', 'REVERSE(name)': 'melapkb' },
  RowDataPacket { name: 'ujjain', 'REVERSE(name)': 'niajju' }
]

We can see that values in the name column are reversed.

REVERSE Example 2:-

Let's return the reversed values in the district column where district is 'hyd' using Node.js script

CopiedCopy Code

// Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(error) {  
  // Write sql query to return actual values, reverse 
  //values from the district column where district is 'hyd'.
  connection_data.query("SELECT district, REVERSE(district) 
  FROM village2 WHERE district = 'hyd';", function (error, result) {
 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket { district: 'hyd', 'REVERSE(district)': 'dyh' },
  RowDataPacket { district: 'hyd', 'REVERSE(district)': 'dyh' }
]

We can see that values in the district column are reversed with value='hyd'.

Summary

In this post, we seen how to use REVERSE() function on MySQL table in XAMPP Server and also it can be possible to specify WHERE clause with this function.