Home >>Nodejs Tutorial >Node.js MySQL SELECT Unique Record

Node.js MySQL SELECT Unique Record

Node.js MySQL SELECT Unique Record

(WHERE Clause)

Retrieve single data from the "users" table. To Select Signle Data use WHERE Clause.

Build a js file called selectwhere.js that has the following data in the folder example1


var db = require('db');  
var con = db.createConnection({  
host: "localhost",  
user: "root",  
password: "",  
database: "phptpoint"  
});  
con.connect(function(err) {  
if (err) throw err;  
con.query("SELECT * FROM users WHERE id = '1'", function (err, result) {  
if (err) throw err;  
console.log(result);  
});  
});  

Now open terminal command and execute the following instruction:

Node selectwhere.js 

Node.js MySQL Select Wildcard

Retrieve a unique data by using wildcard from the table "users".

Build a js file called selectwildcard.js that has the following data in the folder example1.


var db = require('db');  
var con = db.createConnection({  
host: "localhost",  
user: "root",  
password: "",  
database: "phptpoint"  
});  
con.connect(function(err) {  
if (err) throw err;  
con.query("SELECT * FROM users WHERE name LIKE 'A%'", function (err, reult) {  
if (err) throw err;  
console.log(result);  
});  
});  

Now open terminal command and execute the following instruction:

Node selectwildcard.js 

The record will be retrieved where name begins with A.