AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Where clause


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js MySQL Where clause

How to filter the rows of a query selection using WHEREusing node.js?

In this chapter, we'll learn how to filter the rows of a query selection using WHERE, examples.

Syntax:

SELECT FROM [table_name] WHERE [column_name}

Now you have downloaded and installed a mysql database driver.

Once installed this Node.js module can be used to manipulate the MySQL database:

var mysql = require('mysql');

 

Node.js MySQL WHERE with filter applied on a column

When selecting records from a table, using filter "WHERE" statement:

Select records from “Users” Table with filter under “my_db_name”.

Example: WHERE filter applied on a column From Table "Users":

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});
conn.connect(function(err) {
if (err) throw err;
conn.query("SELECT * FROM Users WHERE address = 'Avenida 38'", function (err, result) {
if (err) throw err;
console.log(result);
});
});

Save the code above in a file named "demo_test_db_where.js", and run the file:

E:\Users\Your_Name>node demo_test_db_where.js
Note: "SELECT * FROM Users" will return all rows with the same address from Users table

After run the file will give you this result:

[
{ id: 11, name: 'Jack', address: 'Avenida 38'},
{ id: 24, name: 'Jony', address: 'Avenida 38'},
{ id: 31, name: 'Maria', address: 'Avenida 38'},
{ id: 46, name: 'Chris', address: 'Avenida 38'},
{ id: 54, name: 'Micha', address: 'Avenida 38'}
]

 

Related subjects:
node.js create database nodejs create table node.js MySQL select from upload files

 

Tags: Node.js MySQL Where in, node js mysql select where
How do I connect to node js in mysql?
How do I select a database in node JS?
How do I create a node js database in mysql?
How do I start mysql in node JS?

 

Node.js MySQL WHERE with filter applied on two columns

In the next example, we apply a filter, fetch only those records with ID greater than 30 and address: Avenida 38 from MySQL table Users.

Example: filter applied on two columns:

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

// make to connection to the database.
conn.connect(function(err) {
if (err) throw err;
// if connection is successful
conn.query("SELECT * FROM Users where id>300 && address = 'Avenida 38'", function (err, result, fields) {
// if any error while executing above query, throw error
if (err) throw err;
// if there is no error, you have the result
console.log(result);
});
});

Save the code above in a file called "demo_test_db_where2.js", and run the file:

Example: : Run "demo_test_db_where2.js"
E:\Users\Your_Name>node demo_test_db_where2.js

After run the file will give you this result:

[
{ id: 31, name: 'Maria', address: 'Avenida 38'},
{ id: 46, name: 'Chris', address: 'Avenida 38'},
{ id: 54, name: 'Micha', address: 'Avenida 38'}
]

 

Select specific rous from a column

You can also select rows that starts, includes, or ends with a specific letter or phrase.

Use the '%' wildcard to represent one or multiple characters:

Example:

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

// make to connection to the database.
conn.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM Users WHERE name LIKE 'M%'", function (err, result) {
if (err) throw err;
console.log(result);
});
});

Save the code above in a file called "demo_test_db_where3.js", and run the file:

Example: : Run "demo_test_multi_rows_db_where3.js"
E:\Users\Your_Name>node demo_test_db_where3.js

After run the file will give you this result:

[
{ id: 31, name: 'Maria', address: 'Avenida 38'},
{ id: 54, name: 'Micha', address: 'Avenida 38'}
]

 



Node.js MySQL Where in, node js mysql select where
How do I connect to node js in mysql?
How do I select a database in node JS?
How do I create a node js database in mysql?
How do I start mysql in node JS?
Node.js MySQL Where clause - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...