Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve unique values from a column with Knex.js?

I use Knex.js to communicate to Postgres database.

I have rows in a table with a column named 'state' which represents a US state.

How can I retrieve all unique values from this column?

like image 918
Sergei Basharov Avatar asked Dec 10 '22 09:12

Sergei Basharov


1 Answers

You are probably looking for knex.distinct + knex.pluck combo.

knex
    .distinct()
    .from('tablename')
    .pluck('state')
    .then(states => {
        console.log(states)
    })
like image 101
coockoo Avatar answered Jan 12 '23 17:01

coockoo