Node.js MySQL Where clause
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:
Now you have downloaded and installed a mysql database driver.
Once installed this Node.js module can be used to manipulate the MySQL database:
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”.
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:
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.
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:
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:
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:
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
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

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

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.