Я пытаюсь вставить несколько строк в таблицу с помощью подзапроса, который выбирает значение для определенных полей из другой таблицы.
Это мой код
function(req,res,next){
var data = [
['18ODD2',5,76,'15CSR174','14CST71'],
['18ODD2',5,84,'15CSR169','14CST71']
];
db.query(`INSERT INTO marks(occurrence_id,enrollment_id,teaches_id,marks,remarks) SELECT ?,enrollment_id,?,?,'' FROM enrollments WHERE student_id = ? AND course_id = ?`,[data],function(err,data,fields){
if(err)
console.log(err);
console.log(data);
});
}
Это моя структура таблицы
когда я выполняю код выше, я получаю синтаксическую ошибку
StackTrace
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,'' FROM enrollments WHERE student_id = ? AND course_id = ?' at line 1
at Query.Sequence._packetToError (F:\new-student-connect\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (F:\new-student-connect\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (F:\new-student-connect\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (F:\new-student-connect\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (F:\new-student-connect\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (F:\new-student-connect\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (F:\new-student-connect\node_modules\mysql\lib\Connection.js:502:10)
at Socket.emit (events.js:159:13)
at addChunk (_stream_readable.js:265:12)
at readableAddChunk (_stream_readable.js:252:11)
--------------------
at Protocol._enqueue (F:\new-student-connect\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (F:\new-student-connect\node_modules\mysql\lib\Connection.js:200:25)
at insertMarksBySubject (F:\new-student-connect\controllers\facultyController.js:39:12)
at Layer.handle [as handle_request] (F:\new-student-connect\node_modules\express\lib\router\layer.js:95:5)
at next (F:\new-student-connect\node_modules\express\lib\router\route.js:137:13)
at isAuthenticated (F:\new-student-connect\middlewares\checkAuthenticated.js:7:13)
at Layer.handle [as handle_request] (F:\new-student-connect\node_modules\express\lib\router\layer.js:95:5)
at next (F:\new-student-connect\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\new-student-connect\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\new-student-connect\node_modules\express\lib\router\layer.js:95:5)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'?,?,\'\' FROM enrollments WHERE student_id = ? AND course_id = ?\' at line 1',
sqlState: '42000',
index: 0,
sql: 'INSERT INTO marks(occurrence_id,enrollment_id,teaches_id,marks,remarks) SELECT (\'18ODD2\', 5, 76, \'15CSR174\', \'14CST71\'), (\'18ODD2\', 5, 84, \'15CSR169\',
\'14CST71\'),enrollment_id,?,?,\'\' FROM enrollments WHERE student_id = ? AND course_id = ?' }
Является ли мой подход правильным или есть какой-то альтернативный подход, который помогает мне достичь этого?