Knex从多个表中选择

时间:2017-07-05 14:28:49

标签: javascript node.js postgresql knex.js bookshelf.js

我想用knex运行以下SQL:

select * from (
  (select * from foo)
  union all
  (select * from bar)) as biz limit 10 offset 20;

有没有办法在没有knex.raw的情况下执行此操作?

1 个答案:

答案 0 :(得分:3)

knex支持unionunionAll。它的记录

knex.select().from(function() {
    this.select().from('foo')
        .unionAll(function() {
            this.select().from('bar')
        }).as('biz')
}).limit(10).offset(20).toString()

输出:

select * from (select * from `foo` union all select * from `bar`) as `biz` limit 10 offset 20