mysql - mysql-js如何插入带有插入id的批量记录
问题描述
我的用例。
- 首先,我在表中插入一条记录
grn_master
。 - 然后我得到它来向表
insertedId
中插入多条记录grn_detail
。
这是我的代码。
async function create(req, res, next) {
const grn = req.body.grn;
const supplier = req.body.supplier_id; //An object contains objects
const location = req.body.location;
try {
let query = "INSERT INTO grn_master SET customer_supplier_id = ?,location_id =?";
connection.query(query, [supplier, location], (error, results) => {
if (error) {
console.log(error);
res.sendStatus(500);
} else {
const grn_number = results.insertedId;
let query = "INSERT INTO grn_detail SET item_id=?,unit_price=?,qty=?,total=?,grn_master_id=??";
connection.query(query, [grn, grn_number], (error, results) => {
if (error) {
res.sendStatus(500);
console.log(error);
} else {
res.sendStatus(200);
}
})
}
})
} catch (error) {
console.log(error);
res.sendStatus(500);
}
}
如何使用Mysql-js实现这一点
解决方案
推荐阅读
- excel - 组合项目比较和匹配 -VBA
- oop - 在链表中如何产生价值
- php - 如何减少laravel SQS队列的supervisord进程执行之间的延迟
- xml - Azure Logic Apps xpath 未返回值
- javascript - 如何在 Vue.js 中的某些索引处添加空白 div
- 3d - 如何准备附图/图形
- arrays - Ocaml - 将数组作为函数的参数传递
- jupyter-notebook - 如何重置 nbdev_build_docs
- javascript - 使用 React.js 将 API 的数据保存在内存中
- javascript - 不能使用在另一个文件中定义的 php 变量