📜  Node.js MySQL CHAR_LENGTH()函数

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

Node.js MySQL CHAR_LENGTH()函数

CHAR_LENGTH()函数是 MySQL 中的内置函数,用于获取给定字符串中的字符数。

句法:

CHAR_LENGTH(input_string)

参数: 接受一个参数如下:

  • input_string :它是给定的字符串,其长度要确定。

返回值:它返回给定字符串中的字符数。

模块安装:使用以下命令安装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 name, CHAR_LENGTH(name) 
                AS name_length 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 salary, CHAR_LENGTH(salary) 
                AS salary_length FROM publishers";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});


使用以下命令运行index.js文件:

node index.js

输出:

示例 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 salary, CHAR_LENGTH(salary) 
                AS salary_length FROM publishers";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});

使用以下命令运行index.js文件:

node index.js

输出: