📜  Node.js MySQL CHARACTER_LENGTH()函数

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

Node.js MySQL CHARACTER_LENGTH()函数

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

句法:

CHARACTER_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 CHARACTER_LENGTH('Geeks for Geeks') AS output";
  
    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 CHARACTER_LENGTH(name) AS name_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 CHARACTER_LENGTH(name) AS name_length FROM publishers";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});

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

node index.js

输出: