Node.js MySQL - REPEAT()
In this post, we will discuss how to repeat the same value single/multiple times in a column of MySQL database in XAMPP Server through Node.js application with REPEAT() function.
It is important to install mysql package in node.js.
Command to install the mysql package:
npm install mysql
REPEAT() Function:
REPEAT() will return the same value single or multiple times from a column in a table. It takes two parameters. The first parameter is the column name that hold values and second parameter is the count which is an integer to return the values.
Syntax:
REPEAT(column,count)
Steps for Node.js script:Now let's see steps
- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- Write the sql query that uses REPEAT() function
- Now type the following command in your command prompt to run the script. node file_name.js
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
connection_data.connect(function(error) {
connection_data.query("SELECT REPEAT(column,count),.... FROM
table_name WHERE condition/s...", function (error, result) {
console.log(result);
});
});
Consider the village2 table with the following records:
REPEAT Example 1:-
Let's repeat values in the name column 4 times.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"facility"
});
connection_data.connect(function(error) {
// Write sql query to repeat the values in name column 4 times.
connection_data.query("SELECT name,district,REPEAT(name,4) FROM
village2", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket {
name: 'pnp',
district: 'hyd',
'REPEAT(name,4)': 'pnppnppnppnp'
},
RowDataPacket {
name: 'pnp',
district: 'hyd',
'REPEAT(name,4)': 'pnppnppnppnp'
},
RowDataPacket {
name: 'bkpalem',
district: 'np.sagar',
'REPEAT(name,4)': 'bkpalembkpalembkpalembkpalem'
},
RowDataPacket {
name: 'ujjain',
district: 'ujjain',
'REPEAT(name,4)': 'ujjainujjainujjainujjain'
}
]
Values in name column were repeated 4 times.
REPEAT Example 2:-
Let's repeat values in the district column 2 times where district-'hyd'.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"facility"
});
connection_data.connect(function(error) {
// Write sql query to repeat the values in district column 2 times.
connection_data.query("SELECT name,district,REPEAT(district,2) FROM
village2 where district='hyd'", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket {
name: 'pnp',
district: 'hyd',
'REPEAT(district,2)': 'hydhyd'
},
RowDataPacket {
name: 'pnp',
district: 'hyd',
'REPEAT(district,2)': 'hydhyd'
}
]
There are only 2 values with district-'hyd'. so hyd repeated 2 times.
SummaryIn this post, we seen how to use REPEAT() function on MySQL table in XAMPP Server and also it can be possible to specify WHERE clause along this function.