Node.js MySQL LPAD()函数
LPAD()函数是 MySQL 中的内置函数,用于将给定字符串从左侧附加到文本,直到总长度等于给定值。如果给定的字符串很小,则重复多次。
句法:
LPAD(text, total_length, string)
参数: LPAD()函数接受三个参数,如上所述和如下所述。
- text:在此文本中,值将从左侧填充
- 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);
});
});
输出: