【问题标题】:insert more than 1 value using knex with raw使用带有 raw 的 knex 插入超过 1 个值
【发布时间】:2019-02-08 02:23:43
【问题描述】:

我浏览了 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" = ? 的第二个值??

目标是输出什么都不返回...

【问题讨论】:

    标签: sqlite knex.js


    【解决方案1】:

    是的,你可以。另见documentation link

    您缺少的关键是正确传递参数。请参阅位置数组以传递变量:.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()一样。

    【讨论】:

    • thx,我实际上得到了一些不同的结果,因为我发现我实际查询的信息不正确,哈哈。但我会给你功劳。干得好。
    猜你喜欢
    • 2020-04-17
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    相关资源
    最近更新 更多