Node.js MySQL - Trigonometric functions
In this post, we will discuss how to perform Trigonometric functions on MySQL XAMPP Server through Node.js application with different functions
It is important to install mysql package in node.js.
Command to install the mysql package:
Copied
npm install mysql
Trigonometric Functions
They are the mathematical functions. We will see one by one.
Syntax:
- SIN(column) - Return the Sine value
- COS(column) - Return the Cosine value
- TAN(column) - Return the Tangent value
- COT(column) - Return the Cotangent value
- ASIN(column) - Return the Arc Sine value
- ACOS(column) - Return the Arc Cosine value
- ATAN(column) - Return the Arc Tangent value
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 Trigonometric functions
- Now type the following command in your command prompt to run the script. node file_name.js
Copied
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
Copied
connection_data.connect(function(error) {
connection_data.query("SELECT TRIGINIMETRIC-FUNCTIONS(column1,column2),....
FROM table_name WHERE condition/s...", function (error, result) {
console.log(result);
});
});
Consider the details table with the following records:
Trignometric function Example 1:-
Let's return sine,cosine,tangent and cotangent values of other1 column.
Copied
// 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 Trigonometric functions.
connection_data.query("SELECT other1, SIN(other1),COS(other1),TAN(other1),
COT(other1) from details", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
Copied
[
RowDataPacket {
other1: 7,
'SIN(other1)': 0.6569865987187891,
'COS(other1)': 0.7539022543433046,
'TAN(other1)': 0.8714479827243188,
'COT(other1)': 1.1475154224051356
},
RowDataPacket {
other1: 2,
'SIN(other1)': 0.9092974268256817,
'COS(other1)': -0.4161468365471424,
'TAN(other1)': -2.185039863261519,
'COT(other1)': -0.45765755436028577
},
RowDataPacket {
other1: 6,
'SIN(other1)': -0.27941549819892586,
'COS(other1)': 0.960170286650366,
'TAN(other1)': -0.29100619138474915,
'COT(other1)': -3.436353004180128
},
RowDataPacket {
other1: 37,
'SIN(other1)': -0.6435381333569995,
'COS(other1)': 0.7654140519453433,
'TAN(other1)': -0.8407712554027598,
'COT(other1)': -1.1893841441105928
}
]
Trignometric function Example 2:-
Let's return arc-sine,arc-cosine and arc-tangent values of other1 column.
Copied
// 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 Trigonometric functions.
connection_data.query("SELECT other1, ASIN(other1),ACOS(other1),ATAN(other1)
from details", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
Copied
[
RowDataPacket {
other1: 7,
'ASIN(other1)': null,
'ACOS(other1)': null,
'ATAN(other1)': 1.4288992721907328
},
RowDataPacket {
other1: 2,
'ASIN(other1)': null,
'ACOS(other1)': null,
'ATAN(other1)': 1.1071487177940904
},
RowDataPacket {
other1: 6,
'ASIN(other1)': null,
'ACOS(other1)': null,
'ATAN(other1)': 1.4056476493802699
},
RowDataPacket {
other1: 37,
'ASIN(other1)': null,
'ACOS(other1)': null,
'ATAN(other1)': 1.5437758776076318
}
]
Summary
In this post, we seen how to use Trigonometric functions on MySQL table in XAMPP Server with node.js script.