📜  Node.js MySQL LENGTH()函数

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

Node.js MySQL LENGTH()函数

LENGTH()函数是 MySQL 中的内置函数,用于获取给定字符串的长度(以字节为单位)。

句法:

LENGTH(input_string)

参数: LENGTH()函数接受单个参数,如上所述和如下所述。

  • input_string:我们将得到这个字符串的字符数

返回值: LENGTH()函数返回给定字符串的长度(以字节为单位)。

模块:

  • mysql:处理 MySQL 连接和查询
npm install mysql

SQL 发布者表预览:

示例 1:

Javascript
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 LENGTH('Geeks for Geeks') AS length_in_bytes`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});


Javascript
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, LENGTH(name) AS length_in_bytes FROM publishers`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});


输出:

示例 2:

Javascript

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, LENGTH(name) AS length_in_bytes FROM publishers`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

输出: