English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Node.js MySQL WHERE is used to filter the selection of records in the MySQL SELECT FROM statement based on conditions applied to one or more columns of TABLE.
We will learn to filter records of the table using the following Node.js example
Node.js MySQL WHERE example with filter applied to a column
Example of Node.js MySQL using escaped query values
Example of Node.js MySQL WHERE, filter applied to two columns
In this section's example, we will use the following MySQL table [DATABASE: studentDB, table: students]
selectFromWhere.js
// Si importa il modulo mysql var mysql = require('mysql'); // Si crea una variabile di connessione con dettagli richiesti var con = mysql.createConnection({ host: "localhost", // Indirizzo IP del server MySQL in esecuzione user: "arjun", // Nome utente del database MySQL password: "password", // La password corrispondente database: "studentsDB" // Utilizza il database specificato }); // Si stabilisce una connessione con il database. con.connect(function(err) { if (err) throw err; // Se la connessione è riuscita con.query("SELECT * FROM students where marks>90", function err, result, fields) { // Se si verificano errori durante l'esecuzione della query sopra, viene lanciato un errore if (err) throw err; // Se non ci sono errori, otterrai i risultati console.log(result); }); });
Apri un terminale dalla posizione del file .js in alto e esegui il programma di esempio Node.js MySQL selectFromWhere.js.
WhereEscapingValues.js
// Si importa il modulo mysql var mysql = require('mysql'); // Si crea una variabile di connessione con dettagli richiesti var con = mysql.createConnection({ host: "localhost", // Indirizzo IP del server MySQL in esecuzione user: "arjun", // Nome utente del database MySQL password: "password", // La password corrispondente database: "studentsDB" // Utilizza il database specificato }); // Si stabilisce una connessione con il database. con.connect(function(err) { if (err) throw err; // Se la connessione è riuscita var name = "Bruce Wane"; var query = "SELECT * FROM students where name=" + mysql.escape(name); con.query(query, function err, result, fields) { // Se si verificano errori durante l'esecuzione della query sopra, viene lanciato un errore if (err) throw err; // Se non ci sono errori, otterrai i risultati console.log(result); }); });
Apri un terminale dalla posizione del file .js in alto e esegui il programma di esempio Node.js MySQL WhereEscapingValues.js.
selectFromWhere2.js
// Si importa il modulo mysql var mysql = require('mysql'); // Si crea una variabile di connessione con dettagli richiesti var con = mysql.createConnection({ host: "localhost", // Indirizzo IP del server MySQL in esecuzione user: "arjun", // Nome utente del database MySQL password: "password", // La password corrispondente database: "studentsDB" // Utilizza il database specificato }); // Si stabilisce una connessione con il database. con.connect(function(err) { if (err) throw err; // Se la connessione è riuscita con.query("SELECT * FROM students where marks>90 && rollno<8", function(err, result, fields) { // Se si verificano errori durante l'esecuzione della query sopra, viene lanciato un errore if (err) throw err; // Se non ci sono errori, otterrai i risultati console.log(result); }); });
Apri un terminale dalla posizione del file .js in alto e esegui il programma di esempio Node.js MySQL selectFromWhere.js.
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node selectFromWhere2.js [RowDataPacket { name: 'Raja', rollno: 5, marks: 94 }]
Nella parte Node.js MySQL modulo di questo tutorial Node.js – Node.js MySQL WHERE – abbiamo imparato a filtrare le selezioni delle righe del comando SELECT FROM MySQL basandoci su una o più colonne applicate alla tabella MySQL.