【问题标题】:How to use IS NOT NULL in Knex JS如何在 Knex JS 中使用 IS NOT NULL
【发布时间】:2017-01-20 18:59:34
【问题描述】:

我正在尝试使用 knex 创建以下查询:

SELECT * FROM users group by users.location having users.photo is not null

如下:

knex("users").groupBy("users.location").having("users.photo", "IS NOT", "Null")

我收到以下错误:

The operator IS NOT is not permitted

我浏览了他们的文档,找不到任何有用的东西。

【问题讨论】:

    标签: javascript mysql node.js knex.js


    【解决方案1】:

    你试过了吗:

    knex("users").whereNotNull("photo").groupBy("location")

    【讨论】:

    • 我没试过,但这里不应该使用从句吗?
    • @aitchkhan 你可以链接命令。例如:knex('table').where({"something":"else"}).whereNull("one_column").whereNotNull("some_column") 将创建查询 select * from "table" where "something" = 'else' and "one_column" is null and "some_column" is not null。您可以在这里使用 API:michaelavila.com/knex-querylab
    • 好吧,我没想到会这样。有趣
    【解决方案2】:

    文档有答案。有whereNullwhereNotNullhavingNullhavingNotNull等。

    来自DOCS

    havingNull — .havingNull(column)
    向查询中添加haveNull 子句。

    knex.select('*').from('users').havingNull('email')
    

    输出:

    select * from `users` having `email` is null
    

    havingNotNull — .havingNotNull(column)
    向查询中添加haveNotNull 子句。

    knex.select('*').from('users').havingNotNull('email')
    

    输出:

    select * from `users` having `email` is not null
    

    使用 knex 查询实验室尝试一下:http://michaelavila.com/knex-querylab/

    【讨论】:

      【解决方案3】:

      根据docs.haveRaw 是您所需要的:

      knex("users").groupBy("users.location").havingRaw("users.photo IS NOT ?", [null]);
      

      另一方面,请立即执行 knex.raw,除非在此特定情况下使用构建器还有任何剩余优势。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-12
        • 1970-01-01
        • 2012-01-29
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 2022-11-21
        相关资源
        最近更新 更多