Javascript 有条件地在查询中添加Postgres/Sequelize-Append子句
我有一张有一些记录的桌子。我必须在两个日期之间或给定日期之后/之前过滤这些记录,如果没有通过任何日期,则根本不过滤。日期由服务器路由负载确定。查询是这样的:Javascript 有条件地在查询中添加Postgres/Sequelize-Append子句,javascript,node.js,postgresql,typescript,sequelize.js,Javascript,Node.js,Postgresql,Typescript,Sequelize.js,我有一张有一些记录的桌子。我必须在两个日期之间或给定日期之后/之前过滤这些记录,如果没有通过任何日期,则根本不过滤。日期由服务器路由负载确定。查询是这样的: select * from table where "id"=4 and (s2."createdAt" >= date("createdAt") and s2."createdAt" <= date("createdAt")) 你可以做: where id = :user.id and (:param_fr
select * from table where "id"=4 and (s2."createdAt" >= date("createdAt") and s2."createdAt" <= date("createdAt"))
你可以做:
where
id = :user.id
and (:param_from_date is null or s2."createdAt" >= :param_from_date)
and (:param_to_date is null or s2."createdAt" <= :param_to_date)
在哪里
id=:user.id
和(:param_from_date为null或s2.“createdAt”>=:param_from_date)
和(:param_to_date为null或s2.“createdAt”=合并(:param_from_date,s2.“createdAt”)
s2.“创建数据”
where
id = :user_id
and s2."createdAt" >= coalesce(:param_from_date, s2."createdAt")
and s2."createdAt" <= coalesce(:param_to_date, s2."createdAt")