Node.js Tutorial

Node.js MySQL SELECT Unique Record (WHERE Clause)

Node.js MySQL SELECT Unique Record (WHERE Clause)
Menu

Node.js MySQL SELECT Unique Record

In this tutorial, well 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. Well also explore how to use wildcards in SQL queries to fetch records that match a particular pattern.

Data Science Tutorial:

1. Retrieving a Unique Record Using WHERE Clause

To begin, lets 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.

Heres 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:

  • 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 the employees table 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, lets see how to use wildcards to filter records dynamically based on a pattern.
Create another file named selectwildcard.js in the same DBexample folder.

Heres 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 :

  • The LIKE operator 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, youve 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: 
Machine Learning Tutorial:
Complete Advance AI topics:- 
Complete Python Course with Advance topics:-


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

    Source Code Available

    Interested in This Project?

    Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

    Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
    Chat on WhatsApp

    We usually reply within a few minutes

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Chat with us