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:
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
- 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 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.
- 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 column/s FROM table_name WHERE
condition1 OR condition2 OR .....)", function (error, result) {
console.log(result);
});
});
It will return the records one by one in the following format:
[
RowDataPacket {
column1: value,
column2: value,
.....,
.....
},
RowDataPacket {
column1: value,
column2: value,
.....,
.....
},
.....
.....
Here, each RowDataPacket represent a row.
Consider the village table with the following records:
Example 1:- Single OR
Node.js Script
- To select rows with village_name as 'delhi' or people greater than 20.
- To select rows with distcict as 'guntur' or people less than 50.
// 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:
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.
// 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:
[
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.
SummarySo 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.