web-dev-qa-db-ja.com

使用方法IS KnexJSでNOTNULL

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_

私は彼らのドキュメントを調べましたが、役に立つものは何も見つかりませんでした。

6

docs によると、。havingRawが必要です。

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

一方、この特定のケースでビルダーを使用する利点が残っていない限り、knex.rawを一度に実行します。

0
Sombriks

やってみました:

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

16
alexi2

ドキュメントに答えがあります。 whereNullwhereNotNullhavingNullhavingNotNullなどがあります。

[〜#〜] docs [〜#〜] から:

havingNull— .havingNull(column)
havingNull句をクエリに追加します。

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

出力:

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

havingNotNull— .havingNotNull(column)
havingNotNull句をクエリに追加します。

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

出力:

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

Knexクエリラボを使用して試してみてください: http://michaelavila.com/knex-querylab/

1
Marco