Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将SQL查询转换为KnexJS_Sql_Knex.js - Fatal编程技术网

将SQL查询转换为KnexJS

将SQL查询转换为KnexJS,sql,knex.js,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, cou

我试图将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')
    })
})

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

.on('query-error', function(ex, obj) {
    console.log("KNEX query-error ex:", ex, "obj:", obj);
})
这将在查询崩溃时为您输出生成的SQL。这可能会告诉你陈述的哪一部分是错误的


祝你好运。

谢谢你,加丽尔。事实上,我自己已经想好了,但对于下面的问题,我还有一个问题要问。count(db.raw('case when finish\u time不为null,则1 else null end'))我想在那里创建alias,并尝试了一些方法,但这些方法都不起作用。这里有3个我试过的例子。你能告诉我如何在那里生成别名吗?.count(db.raw('case when finish\u time not null,然后1个else null end作为count\u finished')).count(db.raw('case when finish\u time not null,然后1个else null end)作为count\u finished')).count(db.raw('('case when finish\u time not null然后1 else null end')作为count\u finished')
。选择(db.raw(“count(case when finish\u time not null然后1 else null end)作为count\u finished”)
…knex
语句添加了一个括号层,这会把您弄得一团糟。
The following query worked for me: 

.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')
          .select( db.raw("count(case when finish_time is not null then 1 else null end) as count_finished"))
          .where('store_id', 100)
          .groupBy('date')
          .orderBy('count_finished', 'asc')
          .orderBy('date', 'desc')
          .as('get_max_date')
      })
  });