📜  NodeJS MySQL Max()函数

📅  最后修改于: 2022-05-13 01:56:20.215000             🧑  作者: Mango

NodeJS MySQL Max()函数

我们将在 MySQL 中使用Max()函数来获取某个特定列的最大值。

句法:

MAX(column_name)

参数: MAX()函数接受一个参数,如上文所述和下文所述。

  • column_name:我们必须从中返回最大值的列名。

返回值: MAX()函数返回表中特定列的最大值。

模块安装:使用以下命令安装 MySQL 模块。

npm install mysql

数据库:我们的带有示例数据的 SQL 发布者表预览如下所示。

示例 1:

idex.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 MAX(salary) AS max_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 MAX(salary) AS max_salary 
                 FROM publishers WHERE id < 8";
  
    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 MAX(salary) AS max_salary 
                 FROM publishers WHERE id < 8";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});

输出: