admin管理员组文章数量:1320888
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) {
本文标签: mysqlJavaScript Array in a SQL queryStack Overflow
版权声明:本文标题:mysql - JavaScript Array in a SQL query? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742013436a2413324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论