Bulk insert mysql but from arrays of objects

人走茶凉 提交于 2019-12-13 06:50:01

问题


I've looked into this answer

What I'm looking for is to be able to insert rows from a previous select query in nodejs and use the same RowDataPacket and pass it to another insert query.

For instance, result from previous select call

[ RowDataPacket { user_id: 1024, session_id: 3, notification_id: 1 },
    RowDataPacket { user_id: 1028, session_id: 3, notification_id: 2 } ] ]

following insert

var sql = 'INSERT INTO user_session_notification(user_id, session_id, notification_id) VALUES ?'
var values = []; // ???

回答1:


are you open to use ORM ? if yes you can use Sequelize ORM for node js with mysql driver . This ORM has feature for bulk insert click this link for to see documentation bulk insert. if no you need get the data from select and then merge the data (object) to array before bulk insert .. Hopefully can help you



来源:https://stackoverflow.com/questions/39563025/bulk-insert-mysql-but-from-arrays-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!