【发布时间】:2020-02-08 19:03:44
【问题描述】:
这是我的代码 我想使用 regexp_replace 所以我尝试使用 Knex.raw 但我发现很难使用 where 子句
async function test () {
let usernameData = {
oldUserName: 'MobileOpsteam',
newUserName: 'cool'
}
const oldUserNameRegex = `@${usernameData.oldUserName}(?![a-zA-Z0-9])`
const newUserName = `@${usernameData.newUserName}`
let subQuery = await knex.select('id').from('user_notification').where('title', 'like', `%${usernameData.oldUserName}%`)
let result = await knex.raw((`update user_notification set title = regexp_replace(title, ${oldUserNameRegex}, ${newUserName}, 'g')`).whereIn('id', subQuery))
// let result = await knex.raw(`update user_notification set title = regexp_replace(title, ${oldUserNameRegex}, ${newUserName}, 'g') where id in ${subQuery}`)
console.log(result)
}
【问题讨论】:
标签: node.js postgresql knex.js