Node.js MySQL - SQRT()
In this post, we will discuss how to return square root of values from a column in MySQL XAMPP Server through Node.js application with SQRT() function.
It is important to install mysql package in node.js.
Command to install the mysql package:
npm install mysql
SQRT() Function:
SQRT() will return the square root of values in a table. It takes column name as a parameter.
Syntax:
SQRT(column)
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 SQRT() 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 SQRT(column),.... FROM table_name
WHERE condition/s...", function (error, result) {
console.log(result);
});
});
Consider the details table with the following records:
SQRT Example 1:-
Let's return square roots of values in price 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 SQRT
connection_data.query("SELECT price, SQRT(price) from details;",
function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket { price: 12, 'SQRT(price)': 3.4641016151377544 },
RowDataPacket { price: 34, 'SQRT(price)': 5.830951894845301 },
RowDataPacket { price: 23, 'SQRT(price)': 4.795831523312719 },
RowDataPacket { price: 45, 'SQRT(price)': 6.708203932499369 }
]
So, the square root for all 4 rows were returned.
SQRT Example 2:-
Let's return square roots of values in price column with name as facility1.
// 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 SQRT
connection_data.query("SELECT price, SQRT(price) from
details where name='facility1';", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[ RowDataPacket { price: 12, 'SQRT(price)': 3.4641016151377544 } ]
So, the square root of price is returned with name-'facility1'.
SummaryIn this post, we seen how to use SQRT() function on MySQL table in XAMPP Server with 2 examples along with WHERE Clause.