Can a javascript array be used in an SQL query?
I need to pass studentsId as a parameter in the second query.
db.query('SELECT users.* FROM users JOIN classes ON users.class_id=classes.id WHERE classes.id ="' + req.body.classSelect + '" ', function (err, rows) {
// Store all user ids obtained from the first query
var studentsId = [];
for (var i = 0; i < rows.length; i++) {
studentsId.push(rows[i].id);
}
console.log(studentsId);
// Use studentsId in this query
db.query("SELECT users.id, users.first_name, users.last_name,attendances.type_id,attendances.attendance_timestamp FROM users JOIN attendances ON attendances.user_id = users.id WHERE attendance_timestamp BETWEEN '2016-01-01' AND '2016-12-31' AND user_id IN ('"+ studentsId +"') ", function (err, result) {
if (err) {
throw err;
}
console.log(result);
});
});