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
_
私は彼らのドキュメントを調べましたが、役に立つものは何も見つかりませんでした。
docs によると、。havingRawが必要です。
knex("users").groupBy("users.location").havingRaw("users.photo IS NOT ?", [null]);
一方、この特定のケースでビルダーを使用する利点が残っていない限り、knex.rawを一度に実行します。
やってみました:
knex("users").whereNotNull("photo").groupBy("location")
ドキュメントに答えがあります。 whereNull
、whereNotNull
、havingNull
、havingNotNull
などがあります。
[〜#〜] 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/