Node.js - MySQL LIKE

In this post, we will discuss how to select rows based on pattern using LIKE operator from MySQL XAMPP Server with Node.js

It is important to install mysql package in node.js.

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

LIKE Operator

LIKE is an Operator used with WHERE Clause is to return the rows based on the pattern specified.

Steps for Node.js script:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that include LIKE Operator.
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT * FROM table_name WHERE 
      column LIKE 'pattern';", function (error, result) {
         console.log(result);
      });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the village table with the following records:

alt=

LIKE Example 1:-

Let's get the rows based on values with name column that starts with 'p'. So the pattern will be "p--". Here the pattern represent the values that start with p.

CopiedCopy Code

// 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 all the fields 
//from village2 where value in name column starts with p.
  connection_data.query("SELECT * FROM village2 WHERE 
  name LIKE 'p--';", function (error, result) {
 
//Display the records one by one
    console.log(result);
  });
});
Output:
CopiedCopy Code

[
  RowDataPacket { name: 'pnp', district: 'hyd' },
  RowDataPacket { name: 'pnp', district: 'hyd' }
]

LIKE Example 2:-

Let's get the rows based on values with name column that ends with 'n'. So the pattern will be "--n". Here the pattern represent the values that ends with n.

CopiedCopy Code

// 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 all the fields 
//from village2 where value in name column ends with n.
  connection_data.query("SELECT * FROM village2 WHERE 
  name LIKE '--n';", function (error, result) {
 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[ RowDataPacket { name: 'ujjain', district: 'ujjain' } ]

LIKE Example 3:-

Let's get the rows based on values with name column that starts with 'h' and ends with 'd' from the district column. So the pattern will be "h--d".

CopiedCopy Code

// 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) {	
  connection_data.query("SELECT * FROM village2 WHERE 
  district LIKE 'h--d';", function (error, result) { 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket { name: 'pnp', district: 'hyd' },
  RowDataPacket { name: 'pnp', district: 'hyd' }
]
Summary

In this post, we seen how to use LIKE operator with three examples on MySQL XAMPP Server using Node.js script.