I want to query items from Postgres database with knex. In the database, I have two separate columns first_name
and last_name
, but I need to compare them both to a single value which is formatted "last_name, first_name"
.
How can I combine those two fields in a .where
clause?
For example:
const res = await Database.knex
.select()
.from(`${DB_SCHEMA}.t_contacts as contacts`)
.where({
'contacts.email': email
})
.andWhere({
'contacts.status': 'ACTIVE'
});
2
Answers
The easiest solution was to split the given value by ", " and then have a list of firstNames and lastNames, and then have two where queries for two fields.
You can use
whereRaw()
to combine the columns before comparing to your value:You could also split your value and compare its parts to their corresponding columns. You can even let PostgreSQL handle the split