Node.js MySQL - POWER()

In this post, we will discuss how to return value(column1) raised to the power of another value(column2) in MySQL XAMPP Server through Node.js application with POWER() function.

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

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

POWER() Function

It will return value(column1) raised to the power of another value(column2) in table.It takes two parameters.

Syntax:

POWER(column1,column2)

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 POWER() function
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT POWER(column1,column2),.... 
      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 details table with the following records:

alt=

POWER Example 1:-

Let's return the power of price column raised to other1 column.

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 that uses POWER
  connection_data.query("SELECT price,other1, POWER(price,other1) 
  from details;", function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket {
    price: 12,
    other1: 7,
    'POWER(price,other1)': 35831808
  },
  RowDataPacket { price: 34, other1: 2, 'POWER(price,other1)': 1156 },
  RowDataPacket {
    price: 23,
    other1: 6,
    'POWER(price,other1)': 148035889
  },
  RowDataPacket {
    price: 45,
    other1: 37,
    'POWER(price,other1)': 1.4752411218392995e+61
  }
]

So, the price raised to other1 column values were returned.

POWER Example 2:-

Let's return the power of price column raised to other1 column where name is 'facility3'.

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 that uses POWER
  connection_data.query("SELECT price,other1, POWER(price,other1) 
  from details where name='facility3';", function (error, result) { 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket {
    price: 23,
    other1: 6,
    'POWER(price,other1)': 148035889
  }
]

So, the price raised to other1 column value were returned with name - facility1.

Summary

In this post, we seen how to use POWER() function on MySQL table in XAMPP Server with 2 examples along with WHERE Clause.