📜  NodeJS MySQL LTRIM()函数

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

NodeJS MySQL LTRIM()函数

LTRIM()函数是 MySQL 中的一个内置函数,用于修剪输入字符串左侧的所有空格。

句法:

LTRIM(string)

参数: LTRIM()函数接受如上所述和下文所述的单个参数。

  • 字符串:需要从左侧修剪的字符串

返回值:

LTRIM()函数从左侧修剪后返回字符串。



模块:

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

示例 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 LTRIM('   Geeks For Geeks   ') AS LTRIM_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");
  
  // notice the ? in query
  let query = `SELECT LTRIM(?) AS LTRIM_Output`;
  
  // Dynamic Input
  let input_string = "  Pratik Raut";
  
  // Notice Second Dynamic Parameter
  db_con.query(query, input_string, (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");
  
  // notice the ? in query
  let query = `SELECT LTRIM(?) AS LTRIM_Output`;
  
  // Dynamic Input
  let input_string = "  Pratik Raut";
  
  // Notice Second Dynamic Parameter
  db_con.query(query, input_string, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

输出: