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")
文档有答案。有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/
根据文档,.havingRaw是您所需要的:
knex("users").groupBy("users.location").havingRaw("users.photo IS NOT ?", [null]);
Run Code Online (Sandbox Code Playgroud)
另一方面,立即执行 knex.raw,除非在此特定情况下使用构建器还有任何剩余优势。