Knex 从多个表中选择

Gle*_*ift 3 javascript postgresql node.js bookshelf.js knex.js

我想用 knex 运行以下 SQL:

select * from (
  (select * from foo)
  union all
  (select * from bar)) as biz limit 10 offset 20;
Run Code Online (Sandbox Code Playgroud)

有没有办法不用knex.raw

Tua*_*ran 5

knex 确实支持unionunionAll。它已记录在案

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

输出:

select * from (select * from `foo` union all select * from `bar`) as `biz` limit 10 offset 20
Run Code Online (Sandbox Code Playgroud)