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:
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
- 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.
- 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.
- 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 AND condition2 AND .....)", 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 vaillage table with the following records:
Example 1:- Single AND
Node.js Script
- To select rows with village_name as 'delhi' and people greater than 20.
- To select rows with distcict as 'guntur' and 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 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:
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.
// 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:
[
RowDataPacket {
village_name: 'delhi',
distcict: 'delhi',
people: 30
}
]
So we can see that there is only one row that matches three conditions.
SummarySo 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.