Node.js MySQL OR

In this post, we will discuss how to return rows based on the condition specified inside WHERE Clause using OR Operator.

Its possible to specify multiple conditions with OR Operator.

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

Command to install the mysql package:

CopiedCopy Code
npm install mysql			

OR Operator:

OR operator return rows in which any of the condition is true or macthed. If all the conditions are failed, then corresponding will not be returned.

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 OR 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 OR condition2 OR .....)", 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 village table with the following records:

alt=

Example 1:- Single OR

Node.js Script

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

Rows with village_name as delhi or people greater than 20
[
  RowDataPacket {
    village_name: 'Kakumanu',
    distcict: 'Guntur',
    people: 110
  },
  RowDataPacket {
    village_name: 'delhi',
    distcict: 'delhi',
    people: 30
  },
  RowDataPacket {
    village_name: 'patna',
    distcict: 'patna',
    people: 100
  },
  RowDataPacket {
    village_name: 'bapatla',
    distcict: 'guntur',
    people: 40
  },
  RowDataPacket {
    village_name: 'gogulamudi',
    distcict: 'guntur',
    people: 67
  }
]
Rows with distcict as guntur or people less than 50
[
  RowDataPacket {
    village_name: 'Kakumanu',
    distcict: 'Guntur',
    people: 110
  },
  RowDataPacket {
    village_name: 'delhi',
    distcict: 'delhi',
    people: 30
  },
  RowDataPacket {
    village_name: 'bapatla',
    distcict: 'guntur',
    people: 40
  },
  RowDataPacket {
    village_name: 'gogulamudi',
    distcict: 'guntur',
    people: 67
  }
]

So we can see that rows were returned based on the condition matched.

Example 2:- Multiple OR

Node.js Script to seelct rows with village_name as 'delhi', distcict as 'delhi' or 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' or people less than 100
  connection_data.query("SELECT * FROM village WHERE village_name='delhi' 
  OR distcict='delhi' OR 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
  },
  RowDataPacket {
    village_name: 'bapatla',
    distcict: 'guntur',
    people: 40
  },
  RowDataPacket {
    village_name: 'gogulamudi',
    distcict: 'guntur',
    people: 67
  }
]

So we can see that three rows were returned based on the conditions.

Summary

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