mySQL x Knexjs - 在knex子句中选择日期(列)/日期格式

时间:2017-12-21 02:55:37

标签: javascript mysql date

我正在尝试自动化一些我每周都在运行的脚本。

mySQL脚本:

select count(*), date(updated) from myDB.table where itemId = 50 and date(updated) >= "2017-12-01" and date(updated) <= date(now()) group by date(updated)

在knexjs文件中:

const knex = require('knex')({
  client: 'mysql',
  connection: {
    host: myIP,
    port: myPort,
    debug: true,
    dateStrings: true,
    user: user,
    password: pwd,
    database: myDB
  }
})

查询构建部分:

(async () => {
  let data = await knex
 .select('updated')
 .count()
 .from('myDB.table')
 .where('itemId', '=', 50)
 .where('updated', '>=', '2017-12-01')
 .where('updated', '<=', Date())
 .groupByRaw('date(updated)')

  console.log(data)
})()

如何在knexjs语法中在.where().select()子句中添加日期格式化

0 个答案:

没有答案