diff --git a/docs/source/en/tutorials/mysql.md b/docs/source/en/tutorials/mysql.md index 733fb4b052..b9048659bc 100644 --- a/docs/source/en/tutorials/mysql.md +++ b/docs/source/en/tutorials/mysql.md @@ -233,6 +233,25 @@ const result = await this.app.mysql.update('posts', row); // update records in ' => UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE id = 123 ; +// check if update is success or failure +const updateSuccess = result.affectedRows === 1; + +// if primary key is your custom id,such as custom_id,you should config it in `where` +const row = { + name: 'fengmk2', + otherField: 'other field value', // any other fields u want to update + modifiedAt: this.app.mysql.literals.now, // `now()` on db server +}; + +const options = { + where: { + custom_id: 456 + } +}; +const result = await this.app.mysql.update('posts', row, options); // update records in 'posts' + +=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ; + // check if update is success or failure const updateSuccess = result.affectedRows === 1; ``` diff --git a/docs/source/zh-cn/tutorials/mysql.md b/docs/source/zh-cn/tutorials/mysql.md index b5afe48749..ed77cd1e7e 100644 --- a/docs/source/zh-cn/tutorials/mysql.md +++ b/docs/source/zh-cn/tutorials/mysql.md @@ -249,6 +249,25 @@ const result = await this.app.mysql.update('posts', row); // 更新 posts 表中 => UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE id = 123 ; +// 判断更新成功 +const updateSuccess = result.affectedRows === 1; + +// 如果主键是自定义的 ID 名称,如 custom_id,则需要在 `where` 里面配置 +const row = { + name: 'fengmk2', + otherField: 'other field value', // any other fields u want to update + modifiedAt: this.app.mysql.literals.now, // `now()` on db server +}; + +const options = { + where: { + custom_id: 456 + } +}; +const result = await this.app.mysql.update('posts', row, options); // 更新 posts 表中的记录 + +=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ; + // 判断更新成功 const updateSuccess = result.affectedRows === 1; ```