Node.js - MySQL RIGHT JOIN

In this post, we will discuss how to perform RIGHT Join on MySQL tables in XAMPP Server.

It is important to install mysql package in node.js.

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

RIGHT JOIN

RIGHT JOIN join two or more tables by resulting all rows from table2 and only matched rows from table1 with respect to matched rows from table2.

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 RIGHT JOIN
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
    connection_data.query("SELECT columns... FROM table_name1 RIGHT JOIN table_name2 ON 
    table_name1.column = table_name2.column", 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 village table with the following records:

alt=

Consider the field table with the following records:

alt=

RIGHT JOIN Example 1:-

Let's perform right join based on vid column from village and id column from field.

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) {	
// Right Join
  connection_data.query("SELECT village.vid,village.vname,village.people,field.district,field.type 
  FROM village RIGHT JOIN field ON village.vid = field.id", function (error, result) {
	 let text = ""; 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'black'
  },
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'red'
  },
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'black'
  },
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'black'
  },
  RowDataPacket {
    vid: 3,
    vname: 'delhi',
    people: 30,
    district: 'kadapa',
    type: 'red'
  },
  RowDataPacket {
    vid: 3,
    vname: 'delhi',
    people: 30,
    district: 'kadapa',
    type: 'sand'
  },
  RowDataPacket {
    vid: 5,
    vname: 'gogulamudi',
    people: 67,
    district: 'ongole',
    type: 'red'
  },
  RowDataPacket {
    vid: null,
    vname: null,
    people: null,
    district: 'nellore',
    type: 'sand'
  },
  RowDataPacket {
    vid: null,
    vname: null,
    people: null,
    district: 'nellore',
    type: 'sand'
  },
  RowDataPacket {
    vid: null,
    vname: null,
    people: null,
    district: 'visakha',
    type: 'sand'
  },
  RowDataPacket {
    vid: null,
    vname: null,
    people: null,
    district: 'visakha',
    type: 'sand'
  }
]

We can see that all the records from field table were returned and 6,8 Id's are not present in field table. so for these records, null is returned in village table columns.

RIGHT JOIN Example 2:-

Let's perform right join based on vid column from village and id column from field with people greater than 50.

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) {
// Right Join
  connection_data.query("SELECT village.vid,village.vname,village.people,field.district,field.type 
  FROM village RIGHT JOIN field ON village.vid = field.id WHERE people > 50", function (error, result) {
	 let text = ""; 
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'black'
  },
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'red'
  },
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'black'
  },
  RowDataPacket {
    vid: 1,
    vname: 'Kakumanu',
    people: 110,
    district: 'guntur',
    type: 'black'
  },
  RowDataPacket {
    vid: 5,
    vname: 'gogulamudi',
    people: 67,
    district: 'ongole',
    type: 'red'
  }
]
Summary

In this post, we seen how to perform RIGHT JOIN using Node.js in MySQL XAMPP Server. We also seen WHERE Clause along with RIGHT JOIN.