Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm building a dashboard where I will be displaying data from 2 different tables in the same database. I can do this for 1 query but cannot do it for more than 1

What I have tried:

const mysql = require('mysql');
const Q = require('q')


// Connection Pool
const pool = mysql.createPool({
    multipleStatements: true,
    connectionLimit : 100,
    host: process.env.MYSQL_HOST,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE,
})

// View home-data
exports.view = (req, res) => {

    // Connect to database - box 1
pool.getConnection((err, connection) => {
    if(err) throw err; //Not Connected
    console.log('Connected as ID ' +  connection.threadId);

  

    connection.query('SELECT (SELECT COUNT(*)  FROM table1) AS totalRecords1, (SELECT COUNT(*)  FROM table2) AS totalRecords2', (err, rows1, rows2) => {

        // When finished with connection, release it
     connection.release()
        if(!err) {
           let res1 = JSON.parse(JSON.stringify(rows1[0]['totalRecords1']));
           let res2 = JSON.parse(JSON.stringify(rows2[0]['totalRecords2']))
            console.log(`row1: ${res1}, row2 ${res2}`)
           
            res.render('home', {
                rows1: res1,
                rows2: res2})
        }else {
            console.log(err)
            }
        })
    })
  };
Posted
Updated 3-Mar-23 0:45am
Comments
CHill60 3-Mar-23 6:26am    
We'll need more information than this. What are you trying to retrieve, what do the tables look like, give some sample data and the results you would expect from that sample

1 solution

If I run something very similar to your query through an online MySql sandbox (MySQL online - Test SQL queries[^]):
SQL
create table scientist1 (id integer, firstname varchar(100), lastname varchar(100));
insert into scientist1 (id, firstname, lastname) values (1, 'albert', 'einstein');
insert into scientist1 (id, firstname, lastname) values (2, 'isaac', 'newton');
insert into scientist1 (id, firstname, lastname) values (3, 'marie', 'curie');
create table scientist2 (id integer, firstname varchar(100), lastname varchar(100));
insert into scientist2 (id, firstname, lastname) values (1, 'albert', 'einstein');
insert into scientist2 (id, firstname, lastname) values (2, 'isaac', 'newton');
insert into scientist2 (id, firstname, lastname) values (3, 'marie', 'curie');
insert into scientist2 (id, firstname, lastname) values (4, 'marie', 'curie');
insert into scientist2 (id, firstname, lastname) values (5, 'marie', 'curie');
SELECT (SELECT COUNT(*)  FROM scientist1) AS totalRecords1, (SELECT COUNT(*)  FROM scientist2) AS totalRecords2

I get what I expect:
totalRecords1   totalRecords2
      3               5
So ... the query works.

Start by checking your DB and the actual results you get, allow with the log entry to check for failure messages there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900