📜  NodeJS MySQL LPAD()函数

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

NodeJS MySQL LPAD()函数

LPAD()函数是 MySQL 中的一个内置函数,用于将给定的字符串从左侧附加到文本,直到总长度等于给定值。如果给定的字符串很小,则它会重复多次。

句法:

LPAD(text, total_length, string)

参数: LPAD()函数接受上面提到和下面描述的三个参数。

  • 文本:在此文本值将从左侧填充
  • total_length:输出字符串的总长度
  • 字符串:此字符串值将从左侧填充

返回值: LPAD()函数通过从左侧将值附加到文本直到总长度等于给定值来返回字符串。如果值很小,则重复多次。

模块:



  • 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 LPAD(' For Geeks', 23, 'Geeks') AS LPAD_Output`;
  
  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, LPAD(name, 20, 'Geek') 
               AS LPAD_Output 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, LPAD(name, 20, 'Geek') 
               AS LPAD_Output FROM publishers`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

输出: