📜  Node.js MySQL TRIM()函数

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

Node.js MySQL TRIM()函数

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

句法:

TRIM(string)

参数:它需要一个参数,如下所示:

  • 字符串 :要从两边修剪的字符串。

返回值:从两边修剪后返回字符串。

模块安装:使用以下命令安装mysql模块:

npm install mysql

示例 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 TRIM('   Geeks For Geeks   ') AS TRIM_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");
  
    // Notice the ? in query
    let query = `SELECT TRIM(?) AS TRIM_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);
    });
});


使用以下命令运行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");
  
    // Notice the ? in query
    let query = `SELECT TRIM(?) AS TRIM_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);
    });
});

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

node index.js

输出: