node.js - Sequelize 查找最新创建和更新的_at

标签 node.js postgresql sequelize.js findall

我需要在表格中找到最新的条目。它应该同时考虑 created_at 和 updated_at 字段。如果我添加新数据,那么它应该先出现,或者如果我编辑 a 那么它应该先出现。

db.model.findAll({
  order: [ [ 'created_at ', 'DESC' ]]
}).then(function(data){
}); 

使用上面的代码,我只能列出基于 created_at 的条目如何组合 created_at 和 updated_at ?

最佳答案

你可以在 Postgres 中使用 coalesce 函数:

db.model.findAll({
  order: [ [ sequelize.fn('COALESCE', 'updated_at', 'created_at '), 'DESC' ]]
}).then(function(data){
});

合并函数检查第一个参数是否为空。如果不是,它返回它,否则,它返回第二个参数。如果有两个以上的参数,则空检查链。

关于node.js - Sequelize 查找最新创建和更新的_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592767/

相关文章:

javascript - 显示来自 Meteor 1.0 的数据

linux - 安装 npm、nodejs - 使用 laravel gulp

java - 如果值不在 ENUM 中,则更改插入值

sql - 使用 postgres 创建一个包含三个不同表的三列的 View

postgresql - 非公共(public)模式中的 Postgres 外键约束

mysql update_at 使用自 EPOCH 以来的毫秒数作为默认更新

node.js - json Web token 、passport-jwt 及其验证函数

angularjs - 使用 Node.js 和 Angular 更新数据库

node.js - docker-compose : nodejs container not communicating with postgres container

node.js - 使用 sequelize 和 express API 调用存储过程