📜  Node.js MySQL MID()函数

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

Node.js MySQL MID()函数

MID()函数是 MySQL 中的内置函数,用于获取给定范围(含)之间的输入字符串的子字符串。

句法:

MID(input_string, from, length)

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

  • input_string:将计算此输入的子字符串
  • from:子字符串将从这个位置取出
  • 长度:子字符串的长度

返回值:

MID()函数返回给定起始位置和长度之间的输入字符串的子字符串。如果长度超出字符串,则忽略多余的部分。

模块:

  • 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");
  
  // notice the ? in query
  let query = `SELECT MID("Geeks For Geeks", 7, 20) AS MID_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 name, MID(name, 1, 3) AS MID_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");
  
  // notice the ? in query
  let query = `SELECT name, MID(name, 1, 3) AS MID_Output FROM publishers`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

输出: