English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File(FS)

NodeJS Other

Node.js MySQL WHERE

Node.js MySQL WHERE is used to filter the selection of records in the MySQL SELECT FROM statement based on conditions applied to one or more columns of TABLE.

Where is Node.js MySQL

We will learn to filter records of a table using the following Node.js example

  • Node.js MySQL WHERE example with filters applied to a column

  • Node.js MySQL WHERE example of using escaped query values

  • Node.js MySQL WHERE example, filters applied to two columns

In this section's example, we will use the following MySQL table [DATABASE: studentDB, Table: students]

selectFromWhere.js

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username of the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // If the connection is successful
  con.query("SELECT * FROM students where marks>90", function(err, result, fields) { 
    // If any errors occur during the execution of the above query, then throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Open a terminal from the location of the .js file above, then run the Node.js MySQL example program selectFromWhere.js.

WhereEscapingValues.js

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username of the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // If the connection is successful
  var name = "Bruce Wane"; 
  var query = "SELECT * FROM students WHERE name=" + mysql.escape(name); 
  con.query(query, function(err, result, fields) {}} 
    // If any errors occur during the execution of the above query, then throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Open a terminal from the location of the .js file above, then run the Node.js MySQL example program WhereEscapingValues.js.

selectFromWhere2.js

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username of the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // If the connection is successful
  con.query("SELECT * FROM students where marks>90 && rollno<8" 
    // If any errors occur during the execution of the above query, then throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Open a terminal from the location of the .js file above, then run the Node.js MySQL example program selectFromWhere.js.

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node selectFromWhere2.js  
 [ RowDataPacket { name: 'Raja', rollno: 5, marks: 94 }

Conclusion:

In this Node.js tutorial – Node.js MySQL Module section – Node.js MySQL WHERE – we have learned how to filter the selection of records from the MySQL SELECT FROM statement based on one or more columns of a MySQL table.