Home »
Node.js
Display data from two different tables in MYSQL table by using Node.js
In this article, we are going to learn how to display data from two different tables in MYSQL table using Node.js server?
Submitted by Manu Jemini, on December 10, 2017
Prerequisite/recommended:
As we all know from our previous articles, we need to start with our preparation for a server file of node.js where we require the MySQL module to work on MySQL and then we prepare a connection by using mysql.createConnection() method.
Some certain attributes like the host, user, password, and database name should be there for defining inside our create connection method.
Now we are all set to go with our connection, all we need to create an SQL query basically a select query to display all the data from two different tables and after that we can use query() method to execute query statement with a callback in which we can throw an error if any and print the number of records available in MYSQL table.
Database details:
- Hostname: localhost
- Port number: 3306
- Username: root
- Password: 123
- Database: demo
- Tables name: clients, city
Steps, we are need to follow:
- Require MySQL module.
- Create Connection variable using mysql.createConnection() method.
- Connect by using con.connect() method.
- Creating a select SQL query to select data from both the tables.
- Execute query by using .query() method.
- Show result.
Note: Symbol (*) means all in SQL.
Server File
//step-1
var mysql = require('mysql');
//step-2
var con = mysql.createConnection({
host: "127.0.0.1",
user: "root",
password: "123",
database: "demo"
});
//step-3
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "Select A.*, B.* from clients A,city B ";
//step-4
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
Output/result
If you have any query do comment.