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:

CopiedCopy Code
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

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses REPEAT() function
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT REPEAT(column,count),.... FROM 
      table_name WHERE condition/s...", function (error, result) {
         console.log(result);
      });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the village2 table with the following records:

alt=

REPEAT Example 1:-

Let's repeat values in the name column 4 times.

CopiedCopy Code

// 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:

CopiedCopy Code

[
  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'.

CopiedCopy Code

// 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:

CopiedCopy Code

[
  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.

Summary

In 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.