我想构建一个序列化查询,该查询以升序返回行,并且先返回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值。否则它会满足我的要求
答案 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,
})