Node.js MySQL OR Operator
NodeJs: An open-source platform for executing javascript code on the server-side. Also, a javascript runtime built on Chrome’s V8 JavaScript engine. It can be downloaded from here. Mysql An open-source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). It is the most popular language for adding, accessing, and managing content in a database. Here we will use the Mysql as a database for our node application. It can be downloaded from here.
OR operator: This Operator is a logical operator and used to fetch records from the table when either one of the conditions is true. We use OR Operator to set a basic union query in MySQL.
Syntax:
SELECT [column_name(s)] FROM [table_name] WHERE condition1 OR condition2 OR condition3 …
Module:
- mysql: mysql module is used for interaction between the MySQL server and the node.js application.
Installing Module:
npm install mysql
SQL users Table preview:
Example 1:
index.js
Javascript
const mysql = require( "mysql" );
let db_con = mysql.createConnection({
host: "localhost" ,
user: "root" ,
password: '' ,
database: 'gfg_db'
});
db_con.connect((err) => {
if (err) {
console.log( "Database Connection Failed !!!" , err);
return ;
}
console.log( "We are connected to gfg_db database" );
let query = "SELECT * FROM users WHERE id = 3 OR name = 'rohan'" ;
db_con.query(query, (err, rows) => {
if (err) throw err;
console.log(rows);
});
});
|
Run index.js file using below command:
node index.js
Console Output:
Example 2:
index.js
Javascript
const mysql = require( "mysql" );
let db_con = mysql.createConnection({
host: "localhost" ,
user: "root" ,
password: '' ,
database: 'gfg_db'
});
db_con.connect((err) => {
if (err) {
console.log( "Database Connection Failed !!!" , err);
return ;
}
console.log( "We are connected to gfg_db database" );
let query = "SELECT * FROM users WHERE id = 3 OR name = 'rohan' OR address = 'sideway'" ;
db_con.query(query, (err, rows) => {
if (err) throw err;
console.log(rows);
});
});
|
Run index.js file using below command:
node index.js
Console Output:
Last Updated :
07 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...