Skip to content
Related Articles

Related Articles

Improve Article
NodeJs MySQL OR Operator
  • Difficulty Level : Hard
  • Last Updated : 11 Feb, 2021

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




//Importing mysql module
const mysql = require("mysql");
//Creating connection
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");
  
    // Query to be executed
    let query = "SELECT * FROM users WHERE id = 3 OR name = 'rohan'";
     
    //Executing the query
    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




//Importing mysql module
const mysql = require("mysql");
//Creating connection
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");
  
    // Query to be executed
    let query = "SELECT * FROM users WHERE id = 3 OR name = 'rohan' OR address = 'sideway'";
    // Executing query
    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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :