先用空值用Postgresql顺序进行续集

时间:2019-06-02 03:40:51

标签: node.js postgresql sequelize.js

我想构建一个序列化查询,该查询以升序返回行,并且先返回NULL值。

我在发送电子邮件时有一个时间戳记,如果从未发送过电子邮件,则为NULL。

这是执行我想要的事的Postgres查询:

SELECT quote, quote_id
FROM quotes
WHERE book_id = '${book_id}'
ORDER BY last_emailed ASC NULLS FIRST
LIMIT 5

我的续集查询是:

const res = await Quote.findAll({
  attributes: ['quote', 'quote_id'],
  where: { book_id },
  order: [
    ['last_emailed', 'ASC']
  ],
  limit: 5,
})

但是这最后返回所有NULL值。否则它会满足我的要求

1 个答案:

答案 0 :(得分:2)

NULLS FIRST之后添加ASC应该会给您带来预期的结果:

const res = await Quote.findAll({
   attributes: ['quote', 'quote_id'],
   where: { book_id },
   order: [
      ['last_emailed', 'ASC NULLS FIRST']
   ],
   limit: 5,
})