跳到主要内容

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
信息

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
信息

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
信息

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


Glossary

QueryOptions

QueryOptions Specification