将SQL查询转换为KnexJS

时间:2018-10-30 12:24:28

标签: sql knex.js

我正在尝试将SQL查询转换为KnexJS格式,但是当前的KnexJS查询给我以下错误。

  • “ as”堆栈附近或附近的语法错误:错误:“ as”堆栈附近或附近的语法错误

这是原始查询,也是我一直在为KnexJS处理的查询。 请更正我的KnexJS查询。

先谢谢您!

原始SQL查询:

select count(distinct date) 
from task_history
where 
store_id = 100 and date >
(select date from (
select date, count(*) as count_all, count(case when finish_time is not null 
then 1 else null end) as count_finished
from task_history
where store_id = 100 
group by date
order by count_finished, date desc
fetch first row only) as get_max_date)

KnexJS查询:

.table("task_history")
.count(db.raw("'distinct date'"))
.where('store_id', 100)
.where('date', '>', function() {
    this.select('date')
    .from(function() {
        this.select('date')
        .table("task_history")
        .first()
        .count('* as count_all')
        .count(db.raw(`case when finish_time is not null then 1 else null end as count_finished`))
        .where('store_id', 100)
        .groupBy('date')
        .orderBy('count_finished', 'desc')
        .orderBy('date', 'desc')
        .as('get_max_date')
    })
})

2 个答案:

答案 0 :(得分:1)

那是一个复杂的查询。由于您尚未共享SQL结构供其他人尝试相同的查询,因此建议您尝试在查询中包含以下“ debugging”子句:

.on('query-error', function(ex, obj) {
    console.log("KNEX query-error ex:", ex, "obj:", obj);
})

这将在查询崩溃时为您输出生成的SQL。这可能会告诉您该语句的哪一部分不正确。

祝你好运。

答案 1 :(得分:0)

meteor remove blaze-html-templates
相关问题