Node.js MySQL SELECT Unique Record (WHERE Clause)
Node.js MySQL SELECT Unique Record
In this tutorial, we’ll learn how to retrieve specific or unique records from a MySQL table using Node.js. This is particularly useful when you want to fetch data based on certain conditions, such as employee ID, name, or location. We’ll also explore how to use wildcards in SQL queries to fetch records that match a particular pattern.
Data Science Tutorial:–Click Here
1. Retrieving a Unique Record Using WHERE Clause
To begin, let’s create a JavaScript file named selectwhere.js inside a folder called DBexample. This script will connect Node.js to a MySQL database and retrieve a unique record from the employees table based on a specific ID.
Here’s the code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "updategadh"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees WHERE id = '1'", function (err, result) {
if (err) throw err;
console.log(result);
});
});
Explanation:
Introduction to Applied AI:–Click Here
- We first import the MySQL module using
require('mysql'). - Then, we create a connection to the database updategadh using
mysql.createConnection(). - Inside the
connect()method, we check for errors and execute a SQL query:SELECT * FROM employees WHERE id = '1'This query retrieves the record from theemployeestable where the id is equal to 1. - The result is displayed in the console using
console.log(result).
To execute this file, open your command terminal, navigate to the DBexample folder, and run:
node selectwhere.js
This will display the unique employee record with id = 1 in the console.
Download New Real Time Projects :–Click here
2. Retrieving Records Using Wildcard (LIKE Operator)
Now, let’s see how to use wildcards to filter records dynamically based on a pattern.
Create another file named selectwildcard.js in the same DBexample folder.
Here’s the code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "updategadh"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM employees WHERE city LIKE 'A%'", function (err, result) {
if (err) throw err;
console.log(result);
});
});
Explanation:
SQL Tutorial :–Click Here
- The
LIKEoperator in SQL is used for pattern matching. - Here, the query:
SELECT * FROM employees WHERE city LIKE 'A%'retrieves all employee records where the city name starts with the letter ‘A’. - The
%symbol acts as a wildcard representing any number of characters following ‘A’.
To run the script, execute the following command in the terminal:
node selectwildcard.js
This will fetch and display all employee records where the city name begins with A.
Summary
By following this guide, you’ve learned how to use Node.js with MySQL to:
- Connect to a database.
- Retrieve specific records using the WHERE clause.
- Use the LIKE operator and wildcard characters to perform flexible searches.
These methods form the foundation for building more advanced database-driven applications with Node.js and MySQL.
Deep Learning Tutorial:– Click Here
Machine Learning Tutorial:–Click Here
Complete Advance AI topics:- CLICK HERE
Complete Python Course with Advance topics:-Click Here
mysql select unique values from multiple columns select unique sql mysql select without duplicates mysql show unique constraints which mysql keyword is used to sort the result set distinct rows in mysql select unique email mysql mysql count unique mysql select query nodejs, nodejs mysql select, nodejs mysql delete a record, select mysql count value in nodejs, node.js mysql select query example, node js mysql select data, nodejs mysql query, nodejs mysql order by, mysql order byquery nodejs, nodejs mysql pool query, nodejs delete dabase record, nodejs express mysql, nodejs mysql tutorial, nodejs mysql connection pool, mysql update query nodejs, nodejs and mysql tutorials, records mysql, nodejs mysql2, nodejs mysql crud, nodejs + mysql



Post Comment