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:
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
- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- Write the sql query that uses POWER() function
- Now type the following command in your command prompt to run the script. node file_name.js
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
connection_data.connect(function(error) {
connection_data.query("SELECT POWER(column1,column2),....
FROM table_name WHERE condition/s...", function (error, result) {
console.log(result);
});
});
Consider the details table with the following records:
POWER Example 1:-
Let's return the power of price column raised to other1 column.
// 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:
[
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'.
// 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:
[
RowDataPacket {
price: 23,
other1: 6,
'POWER(price,other1)': 148035889
}
]
So, the price raised to other1 column value were returned with name - facility1.
SummaryIn this post, we seen how to use POWER() function on MySQL table in XAMPP Server with 2 examples along with WHERE Clause.