Skip to main content

SELECT

The examples below also work for the execute method.

query(sql)โ€‹

query(sql: string)

try {
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';

const [rows, fields] = await connection.query(sql);

console.log(rows);
console.log(fields);
} catch (err) {
console.log(err);
}
  • rows contains rows returned by server
  • fields contains extra meta data about rows, if available
info

The connection used for the query (.query()) can be obtained through the createConnection, createPool or createPoolCluster methods.


query(options)โ€‹

query(options: QueryOptions)

try {
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';

const [rows, fields] = await connection.query({
sql,
// ... other options
});

console.log(rows);
console.log(fields);
} catch (err) {
console.log(err);
}
  • rows contains rows returned by server
  • fields contains extra meta data about rows, if available
info

The connection used for the query (.query()) can be obtained through the createConnection, createPool or createPoolCluster methods.


query(options) โ€” Row as Arrayโ€‹

query(options: QueryOptions)

try {
const sql = 'SELECT * FROM `users` WHERE `name` = "Page" AND `age` > 45';

const [rows, fields] = await connection.query({
sql,
rowsAsArray: true,
// ... other options
});

console.log(rows);
console.log(fields);
} catch (err) {
console.log(err);
}
  • rows contains rows returned by server as array
  • fields contains extra meta data about rows, if available
info

The connection used for the query (.query()) can be obtained through the createConnection, createPool or createPoolCluster methods.


Glossaryโ€‹

QueryOptionsโ€‹

QueryOptions Specification