使用 knexjs,我如何比较 .where() 函数中的两列?
With knexjs, how do I compare two columns in the .where() function?
仅使用 knexjs(无书架)我想执行类似以下查询的操作:
select * from table1 where column1 < column2
但是,当我这样做时:
.table("table1").select().where("column1", "<", "column2")
knexjs生成的SQL是:
select * from table1 where column1 < 'column2'
它没有给出所需的结果b/c它不是比较列中的值,而是比较字符串的值,'column2'。
有人知道如何做我想做的事吗?谢谢!
好的,经过一些挖掘,看起来可以通过这种方式完成。不确定这是否是最佳做法,但目前它一直有效,直到我听到其他情况...
.table("table1").select().where("column1", "<", knex.raw("table1.column2"))
同样,不理想,但它完成了工作。请务必
import knex from "knex";
在您使用它的任何文件的顶部。
截至 knex 0.15.0(2018 年 7 月),我们现在有:
table("table1").select().where("column1", "<", knex.ref("column2"))
正如 Greg Hornby 在对其他答案的评论中提到的,您还可以在原始查询中使用 ??
绑定到列或 table 名称。
仅使用 knexjs(无书架)我想执行类似以下查询的操作:
select * from table1 where column1 < column2
但是,当我这样做时:
.table("table1").select().where("column1", "<", "column2")
knexjs生成的SQL是:
select * from table1 where column1 < 'column2'
它没有给出所需的结果b/c它不是比较列中的值,而是比较字符串的值,'column2'。
有人知道如何做我想做的事吗?谢谢!
好的,经过一些挖掘,看起来可以通过这种方式完成。不确定这是否是最佳做法,但目前它一直有效,直到我听到其他情况...
.table("table1").select().where("column1", "<", knex.raw("table1.column2"))
同样,不理想,但它完成了工作。请务必
import knex from "knex";
在您使用它的任何文件的顶部。
截至 knex 0.15.0(2018 年 7 月),我们现在有:
table("table1").select().where("column1", "<", knex.ref("column2"))
正如 Greg Hornby 在对其他答案的评论中提到的,您还可以在原始查询中使用 ??
绑定到列或 table 名称。