在 knex js (postgres) 的 SELECT 子句中使用字符串
use a string in SELECT clause on knex js (postgres)
您好,我想在 KnexJS 的 SELECT 子句中包含一个字符串值。它在 Postgresql 上运行良好,但我不确定如何将该查询转换为 Knex 语法。
这是 Postgresql:
select date, 'UNFINISHED' from task_history
where store_id=1 and date=20160527
这是 Knex:
await db.table('task_history')
.count('*')
.select('date', knex.raw("UNFINISHED"))
.where({
store_id: request.params.storeid,
finish_time: null
})
.whereBetween('date', [request.params.sdate, request.params.edate])
.groupBy('date')
Knex one 给我一个错误,说没有名为 UNFINISHED 的列。 "UNFINISHED" 是一个字符串值,我想 return 与查询结果。
我尝试使用 knex.raw() 作为下面提到的第一个解决方案,但不知何故,我无法使用 knex.raw()。也许那是因为 'await'?我不确定。
更新:.select('date', db.raw("'UNFINISHED'")) 只缺少一个单引号。
对于文字字符串,使用 knex.raw()
db.table('task_history')
.select('date', db.raw("UNFINISHED"))
.where({store_id:1, date:20160527})
您好,我想在 KnexJS 的 SELECT 子句中包含一个字符串值。它在 Postgresql 上运行良好,但我不确定如何将该查询转换为 Knex 语法。
这是 Postgresql:
select date, 'UNFINISHED' from task_history
where store_id=1 and date=20160527
这是 Knex:
await db.table('task_history')
.count('*')
.select('date', knex.raw("UNFINISHED"))
.where({
store_id: request.params.storeid,
finish_time: null
})
.whereBetween('date', [request.params.sdate, request.params.edate])
.groupBy('date')
Knex one 给我一个错误,说没有名为 UNFINISHED 的列。 "UNFINISHED" 是一个字符串值,我想 return 与查询结果。
我尝试使用 knex.raw() 作为下面提到的第一个解决方案,但不知何故,我无法使用 knex.raw()。也许那是因为 'await'?我不确定。
更新:.select('date', db.raw("'UNFINISHED'")) 只缺少一个单引号。
对于文字字符串,使用 knex.raw()
db.table('task_history')
.select('date', db.raw("UNFINISHED"))
.where({store_id:1, date:20160527})