Node.js MySQL AND

In this post, we will discuss how to return rows based on the condition specified inside WHERE Clause using AND Operator, It can be possible to specify multiple conditions with AND Operator.

AND operator return rows in which all the conditions are true or matched. If any one condition fails, then corresponding will not be returned

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

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

AND Operator:

AND operator return rows in which all the conditions are true or matched If any one condition fails, then corresponding will not be returned.

Steps:

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. rite the sql query that uses AND Operator to specify the conditions. It will take two parameters. The first parameter is the SQL Query and the second parameter will handle the result.
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
    connection_data.query("SELECT column/s FROM table_name WHERE 
    condition1 AND condition2 AND .....)", 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

It will return the records one by one in the following format:

CopiedCopy Code

[
  RowDataPacket {
    column1: value,
    column2: value,
    .....,
	.....
},

 RowDataPacket {
    column1: value,
    column2: value,
    .....,
	.....
},
.....
.....

Here, each RowDataPacket represent a row.

Consider the vaillage table with the following records:

alt=

Example 1:- Single AND

Node.js Script

  1. To select rows with village_name as 'delhi' and people greater than 20.
  2. To select rows with distcict as 'guntur' and people less than 50.
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 to select rows with village_name 
//as delhi and people greater than 20.
  connection_data.query("SELECT * FROM village WHERE village_name='delhi' 
  AND people>20", function (error, result) {
 
 console.log("Rows with village_name as delhi and people greater than 20");
 
//Display the records one by one
    console.log(result);
  });
  
	// Write SQL query to select rows with distcict as guntur 
	//and people less than 50.
  connection_data.query("SELECT * FROM village WHERE distcict='guntur' 
  AND  people < 50", function (error, result) {
 
 console.log("Rows with distcict as guntur and people less than 50");
 
//Display the records one by one
    console.log(result);
  });
});
Output:
CopiedCopy Code

Rows with village_name as delhi and people greater than 20
[
  RowDataPacket {
    village_name: 'delhi',
    distcict: 'delhi',
    people: 30
  }
]
Rows with distcict as guntur and people less than 50
[
  RowDataPacket {
    village_name: 'bapatla',
    distcict: 'guntur',
    people: 40
  }
]

So we can see that rows were returned based on the condition matched. For second query, Actually there are two rows with distcict as guntur but the second condition is people less than 50. so only one row returned among two rows.

Example 2:- Multiple AND

Node.js Script to select rows with village_name as 'delhi', distcict as 'delhi' and people less than 100.

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 to select rows with village_name as 'delhi', 
// distcict as 'delhi' and people less than 100
  connection_data.query("SELECT * FROM village WHERE village_name='delhi' AND 
  distcict='delhi' AND people < 100", function (error, result) {

 
//Display the records one by one
    console.log(result);
  });
 
});
Output:
CopiedCopy Code

[
  RowDataPacket {
    village_name: 'delhi',
    distcict: 'delhi',
    people: 30
  }
]

So we can see that there is only one row that matches three conditions.

Summary

So we seen how to use AND Operator on a MySQL Table in XAMPP Server using Node.js. It can be possible to specify multiple conditions with AND Operator.