Node.js 在postgresql中,如何使用';其中';在Knex.raw中作为到';其中';是否来自子查询?

Node.js 在postgresql中,如何使用';其中';在Knex.raw中作为到';其中';是否来自子查询?,node.js,postgresql,knex.js,Node.js,Postgresql,Knex.js,这是我的密码 我想使用regexp_replace,所以我尝试使用Knex.raw,但我发现很难使用where子句 async function test () { let usernameData = { oldUserName: 'MobileOpsteam', newUserName: 'cool' } const oldUserNameRegex = `@${usernameData.oldUserName}(?![a-zA-Z0-9])`

这是我的密码 我想使用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)
}

像这样的方法应该会奏效:

  // subquery builder is not awaited, since query should not be executed separately
  let subQuery = knex.select('id')
    .from('user_notification').where('title', 'like', `%${usernameData.oldUserName}%`)

  let result = await knex('user_notification')
      .update({
          title: knex.raw(
                     `regexp_replace(??, ${oldUserNameRegex}, ${newUserName}, 'g')`, 
                      ['title'])
      }).whereIn('id', subQuery)
基本上,
knex.raw()
不能附加任何附加的
.where(…)
等,因为raw builder没有关于它所包含的SQL的语法元素的任何信息