使用 knex 和 raw 插入超过 1 个值



我已经浏览了knex文档,并尝试了我能找到的所有可能的组合,但没有成功。但是,使用 SQLiteStudio 我能够成功地使用多个?进行常规查询......

select * from 'articles' where LOWER("cover_page") = ? AND "cover_page" != "" AND "user_id" = ?

select * from 'articles' where LOWER("title") = ? AND "title" != "" AND "user_id" = ?

select * from 'articles' where LOWER("link") = ? AND "link" != "" AND "user_id" = ?

。在 knex 中,如果我一次只使用 1 ?,我能够获得以下成功

const doesExist = await db('articles')
    .where(db.raw('LOWER("cover_page") = ?', article.cover_page.toLowerCase()))
    .orWhere(db.raw('LOWER("title") = ?', article.title.toLowerCase()))
    .orWhere(db.raw('LOWER("link") = ?', article.link.toLowerCase()))
    .first();

有没有人知道如何在AND "user_id" = ?的地方输入第二个值?

目标是让输出不返回任何内容...

是的,你可以。另请参阅文档链接。

您缺少的键是正确传递参数。请参阅位置数组以传递变量:.whereRaw('LOWER(cover_page) = ? AND LOWER(title) = ?', ['somepage', 'sometitle'])

(我也怀疑你不应该在这种情况下引用你的数据库字段。

我还没有执行它,但这应该很接近:

const doesExist = await db('articles')
   .whereRaw('LOWER(cover_page) = ? OR LOWER(title) = ? OR LOWER(link) = ?',
       [article.cover_page.toLowerCase(), 
        article.title.toLowerCase(),
        article.link.toLowerCase()] ) 
   .first();

PS:您也可以使用位置数组来传递参数以raw()与我将其用于whereRaw()相同。

最新更新