Node.js MySQL CONCAT_WS()函数
CONCAT_WS()函数是 MySQL 中的内置函数,用于将一组字符串与常用的分隔符连接起来。
句法:
CONCAT_WS(separator, string_1, string_2, ...)
参数:它需要两个参数,如下所示:
- 分隔符:此分隔符将用于连接字符串。
- 字符串 :它是由逗号(',')分隔的给定输入字符串的集合。
返回值:它返回一个字符串,该字符串是一组具有公共给定分隔符的字符串的串联。
模块安装:使用以下命令安装mysql模块:
npm install mysql
数据库:我们的 SQL发布者 带有示例数据的表格预览如下所示:
示例 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 CONCAT_WS(' # ', 'Geeks', 'for', 'Geeks') AS 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");
// Here is the query
let query =
"SELECT CONCAT_WS(': $', name, salary) AS Info FROM publishers";
db_con.query(query, (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");
// Here is the query
let query =
"SELECT CONCAT_WS(': $', name, salary) AS Info FROM publishers";
db_con.query(query, (err, rows) => {
if(err) throw err;
console.log(rows);
});
});
使用以下命令运行index.js文件:
node index.js
输出: