Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

586
Views
Having multiple sql queries in nodejs

I have a function that returns me a persons username from my database. I then want to use that returned value to query another table, and return the data from that. what I set up was this:

var select = "select * from USERS WHERE ASSIGNED_TRAINER = ?"

console.log("INSDE getClientList");

  
mysqlconn.connect(function(err) {
      if (err) {
        console.error('Database connection failed: ' + err.stack);
        return;
      }

    mysqlconn.query(select, [req.session.username], function(err, rows) {
        if (err) {
            console.log(err);
        } else {
            
         for (var i = 0; i < rows.length; i++) {
           // need to query the database again, using this value: rows[i].USERNAME
              console.log(rows[i].USERNAME)
         }
            getNumOfSessionForTrainerClientList(req, res, rows[i])
        }
        mysqlconn.end();

    })
})

then I created this function:

function getNumOfSessionForTrainerClientList(req, res, rows) {




  var getNumOfSessionForTrainerClientList = "select * from SCHEDULE WHERE CLIENT_USERNAME = ? AND ASSIGNED_TRAINER = ?"
  mysqlconn.connect(function(err) {
      if (err) {
        console.error('Database connection failed: ' + err.stack);
        return;
      }
    mysqlconn.query(getNumOfSessionForTrainerClientList, [rows.USERNAME, req.session.username], function(err, sessionData) {
        if (err) {
            console.log(err);
        } else {
            //go to checkIfUserPaid
            console.log("Lenght or rows: " + sessionData.length);
            for (var x = 0; x < sessionData.length; x++) {
                console.log(sessionData[x])
            }

        }
        mysqlconn.end();
    })
})

}

however, because there are going to be up to 25 usernames, then i need to loop through the results and query the schedule table. how can I do this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!