如何从具有 Knex.js 的列中检索唯一值?

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

我使用 Knex.js 与 Postgres 数据库通信。

我在 table 中有几行,其中有一列名为 'state',代表美国的一个州。

如何从此列中检索所有唯一值?

您可能正在寻找 knex.distinct + knex.pluck 组合。

knex
    .distinct()
    .from('tablename')
    .pluck('state')
    .then(states => {
        console.log(states)
    })