Merey Nurlan
Merey Nurlan

Reputation: 305

What is the diffrence between .where() and .whereExists() in knex.js?

I believe it is the same question as if asking what is the difference between WHERE and WHERE EXIST ?!

Upvotes: 2

Views: 3243

Answers (1)

richyen
richyen

Reputation: 10038

.where() creates a WHERE clause, as in:

select `id` from `users` where `first_name` = 'Test' and `last_name` = 'User'

This clause is used to begin the conditional portion of a query.

.whereExists() creates a WHERE EXISTS clause, as in:

select * from `users` where exists (select * from `accounts` where users.account_id = accounts.id)

This clause tests for existence of rows in the subquery. In some cases, it is faster than using a JOIN, as it does not join the entire table to the higher-level table (in the FROM clause). More information at this other SO post

(These example queries were taken from the knex website)

Disclosure: I work for EnterpriseDB (EDB)

Upvotes: 2

Related Questions