Is it possible to use a javascript array in a sql query?
I want to use 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) {
// Saves all my users id that i get from my first query
var studentsId = [];
for (var i = 0; i < rows.length; i++) {
studentsId.push(rows[i].id);
}
console.log(studentsId);
// in this query i want to use studentsId
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);
});
});
Is it possible to use a javascript array in a sql query?
I want to use 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) {
// Saves all my users id that i get from my first query
var studentsId = [];
for (var i = 0; i < rows.length; i++) {
studentsId.push(rows[i].id);
}
console.log(studentsId);
// in this query i want to use studentsId
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);
});
});
Share
Improve this question
edited Jan 12, 2017 at 21:27
s0ckan
asked Jan 12, 2017 at 21:22
s0ckans0ckan
571 gold badge1 silver badge7 bronze badges
3
- Only if you send it to the server using for example AJAX – mplungjan Commented Jan 12, 2017 at 21:27
-
Why don't you just bine the two queries with
JOIN
? – Barmar Commented Jan 12, 2017 at 21:30 -
1
Don't put quotes around
studentsId
when you concatenate it. That makes it just a single element in theIN()
list, instead of a list of IDs. – Barmar Commented Jan 12, 2017 at 21:31
2 Answers
Reset to default 5Yes, but not exactly as the way you do it.
I would change
studentsId.push(rows[i].id);
to
studentsId.push('"' + rows[i].id + '"');
Next, change this line
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) {
to this
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.join(',') + ') ", function (err, result) {
Then your code should run fine.
The first change adds the quotes to the ID's. This might not be nessecary if your ID's are numeric.
The second change joins the ID's together using a ma, so they can be used in the on clause.
Just add the JOIN
with attendances
into the first query, instead of making a separate query.
db.query('SELECT users.* FROM users JOIN classes ON users.class_id=classes.id join attendances ON attendances.user_id = users.id WHERE classes.id ="' + req.body.classSelect + '" AND attendance_timestamp BETWEEN "2016-01-01" AND "2016-12-31" ', function (err, rows) {