【问题标题】:Sequelize: Converting String column value to Date before using it in where clauseSequelize:在 where 子句中使用之前将字符串列值转换为日期
【发布时间】:2022-01-12 01:57:57
【问题描述】:

我面临的问题是我无法将列从字符串转换为日期,以便在 Sequelize 的 where 子句中主动使用它。

一个有效的示例 SQL

select * from sold_prices_england where  STR_TO_DATE(lodgement_date, '%d/%m/%Y') > STR_TO_DATE(DYNAMIC_VALUE_HERE,'%d/%m/%Y')

转换列的代码是Sequelize.fn('STR_TO_DATE', Sequelize.col('lodgement_date'), '%d/%m/%Y');,但我要做的是先转换列中的值,然后再在 where 子句中使用它。

这就是我想要达到的目标

.
. // other clauses above and below
.
 lodgement_date: { // but where lodgement_date is a date and not a string
      [Op.gt]:
        Sequelize.fn('STR_TO_DATE', moment(start_date).format('DD/MM/YYYY'), '%d/%m/%Y'),
    },
.
.
.

【问题讨论】:

    标签: mysql node.js orm sequelize.js


    【解决方案1】:

    您可以使用Sequelize.where 和Sequelize.fn 来实现您的条件,如下所示:

    Sequelize.where(
     Sequelize.fn('STR_TO_DATE', Sequelize.col('lodgement_date'), '%d/%m/%Y'),
     Op.gt,
     Sequelize.fn('STR_TO_DATE', moment(start_date).format('DD/MM/YYYY'), '%d/%m/%Y')
    )
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多