Node.js MySQL Min()函数
我们将在 MySQL 中使用Min()函数从某个特定列中获取最小值。
句法:
MIN(column_name)
参数: MIN()函数接受上面提到和下面描述的单个参数。
- column_name:我们必须从中返回最小值的列名。
返回值: MIN()函数返回表中特定列的最小值。
模块安装:使用以下命令安装 MySQL 模块。
npm install mysql
数据库:我们的带有示例数据的 SQL 发布者表预览如下所示。
示例 1:
index.js
const mysql = require("mysql");
let db_con = mysql.createConnection({
host: "localhost",
user: "root",
password: '',
database: 'gfg_db'
});
db_con.connect((err) => {
if (err) {
console.log("Database Connection Failed !!!", err);
return;
}
console.log("We are connected to gfg_db database");
// Here is the query
let query = "SELECT MIN(salary) AS min_salary FROM publishers";
db_con.query(query, (err, rows) => {
if(err) throw err;
console.log(rows);
});
});
index.js
const mysql = require("mysql");
let db_con = mysql.createConnection({
host: "localhost",
user: "root",
password: '',
database: 'gfg_db'
});
db_con.connect((err) => {
if (err) {
console.log("Database Connection Failed !!!", err);
return;
}
console.log("We are connected to gfg_db database");
// Here is the query
let query = "SELECT MIN(salary) AS min_salary
FROM publishers WHERE id < 7";
db_con.query(query, (err, rows) => {
if(err) throw err;
console.log(rows);
});
});
输出:
示例 2:
index.js
const mysql = require("mysql");
let db_con = mysql.createConnection({
host: "localhost",
user: "root",
password: '',
database: 'gfg_db'
});
db_con.connect((err) => {
if (err) {
console.log("Database Connection Failed !!!", err);
return;
}
console.log("We are connected to gfg_db database");
// Here is the query
let query = "SELECT MIN(salary) AS min_salary
FROM publishers WHERE id < 7";
db_con.query(query, (err, rows) => {
if(err) throw err;
console.log(rows);
});
});
输出: