簡體   English   中英

如何在Knex JS中使用IS NOT NULL

[英]How to use IS NOT NULL in 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

我已經閱讀了他們的文檔,找不到任何有用的東西。

你有沒有嘗試過:

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

根據文檔.havingRaw是你需要的:

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

另一方面,除非在此特定情況下使用構建器有任何剩余優勢,否則請立即執行knex.raw。

文檔有答案。 whereNullwhereNotNullhavingNullhavingNotNull等等。

來自DOCS

havingNull - .havingNull(列)
向查詢添加havingNull子句。

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

輸出:

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

havingNotNull - .havingNotNull(列)
向查詢添加havingNotNull子句。

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

輸出:

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

嘗試使用knex查詢實驗室: http ://michaelavila.com/knex-querylab/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM