2

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);
    });
});
3
  • Only if you send it to the server using for example AJAX Commented Jan 12, 2017 at 21:27
  • Why don't you just combine the two queries with JOIN? 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 the IN() list, instead of a list of IDs. Commented Jan 12, 2017 at 21:31

2 Answers 2

5

Yes, 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 comma, so they can be used in the on clause.

Sign up to request clarification or add additional context in comments.

3 Comments

When an array is converted to a string, it automatically joins with comma, so that's effectively what he was doing, except for the added quotes around it.
Wait what, all these years I've been using join for nothing -.-
If you're displaying to the user, you usually want to put a space after the comma.
0

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) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.