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:

CopiedCopy Code
npm install mysql
				

Trigonometric Functions

They are the mathematical functions. We will see one by one.

Syntax:

  1. SIN(column) - Return the Sine value
  2. COS(column) - Return the Cosine value
  3. TAN(column) - Return the Tangent value
  4. COT(column) - Return the Cotangent value
  5. ASIN(column) - Return the Arc Sine value
  6. ACOS(column) - Return the Arc Cosine value
  7. ATAN(column) - Return the Arc Tangent value

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 Trigonometric functions
  8. CopiedCopy Code
    
    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);
      });
    });
    
  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=

Trignometric function Example 1:-

Let's return sine,cosine,tangent and cotangent values of 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 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:

CopiedCopy Code

[
  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.

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 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:

CopiedCopy Code

[
  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.