如何在Knex JS中使用IS NOT NULL

Aks*_*pal 4 javascript mysql node.js knex.js

我正在尝试使用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

我已经阅读了他们的文档,找不到任何有用的东西.

ale*_*xi2 15

你有没有尝试过:

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

  • @aitchkhan,你可以链接命令。例如: `knex('table').where({"something":"else"}).whereNull("one_column").whereNotNull("some_column")` 将创建查询 `select * from "table" where “something”=“else”且“one_column”为空且“some_column”不为空`。您可以在这里使用 API:http://michaelavila.com/knex-querylab/ (2认同)

Mar*_*rco 5

文档有答案。有whereNull, whereNotNull, havingNull,havingNotNull等等。

文档

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

knex.select('*').from('users').havingNull('email')
Run Code Online (Sandbox Code Playgroud)

输出:

select * from `users` having `email` is null
Run Code Online (Sandbox Code Playgroud)

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

knex.select('*').from('users').havingNotNull('email')
Run Code Online (Sandbox Code Playgroud)

输出:

select * from `users` having `email` is not null
Run Code Online (Sandbox Code Playgroud)

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


Som*_*iks 2

根据文档.havingRaw是您所需要的:

knex("users").groupBy("users.location").havingRaw("users.photo IS NOT ?", [null]);
Run Code Online (Sandbox Code Playgroud)

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