Node.js - MySQL WILD CARDS
In this post, we will discuss how to select rows based on wild card 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:
npm install mysql
LIKE Wild Card
LIKE is an Operator used with WHERE Clause is to return the rows based on the wild cards specified. wild card is used to substitute single or multiple characters in a column. We will discuss in details with some examples.
Steps for Node.js script: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.
- Write the sql query that include LIKE Operator with wild cards.
- 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 * FROM table_name WHERE column
LIKE 'wildcard';", function (error, result) {
console.log(result);
});
});
Consider the village2 table with some records:
WILD CARDS Example 1:-
Let's get the rows based on values with district column that starts any two characters followed by d. Here the wildcard is '_'. so the pattern is '__d'. We need to specify two underscores wildcards.
// 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 '__d';", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket { name: 'pnp', district: 'hyd' },
RowDataPacket { name: 'pnp', district: 'hyd' }
]
WILD CARDS Example 2:-
Let's get the rows based on values with district column that starts with h and followed with two underscores(__) Here the wildcard is '_'. so the pattern is 'h__'. We need to specify two underscores wildcards after h.
// 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__';", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket { name: 'pnp', district: 'hyd' },
RowDataPacket { name: 'pnp', district: 'hyd' }
]
Summary
In this post, we seen how to use LIKE operator with wild cards on MySQL XAMPP Server using Node.js script.