2017-11-08 3 views
1

で複数のテーブルから選択します:knex.jsとknex.jsこのクエリを実行する方法

select * from table1, table2; 

私はこれを試してみた:

const knex = require('knex')({ 
    client: 'sqlite3', 
    connection: { filename: ':memory:' }, 
}) 

async function main() { 
    await knex.schema.createTable('table1', table => table.string('field1')) 
    await knex.schema.createTable('table2', table => table.string('field2')) 
    await knex.insert({ field1: 'value' }).into('table1') 
    await knex.insert({ field2: 'value' }).into('table2') 
    await knex.from('table1', 'table2') // first attempt 
    await knex.from(['table1', 'table2']) // second attempt 
} 

main().catch(err => { 
    console.error(err) 
}) 

私はENV DEBUG='knex:query'でそのコードを実行し、この出力を得た:

create table `table1` (`field1` varchar(255)) 
create table `table2` (`field2` varchar(255)) 
insert into `table1` (`field1`) values (?) 
insert into `table2` (`field2`) values (?) 
select * from `table1` 
select * from `table1` as `0`, `table2` as `1` 

は明らかに、select文は私が期待したものではありません。

PS:あなたはこの質問をマークした場合の

はそうではない、this oneの複製です。 質問の主な点が異なり、回答はthat question で私の質問には答えません。

select * from table1, table2; 
select * from table1 cross join table2; 

答えて

0
knex.from('table1').crossJoin('table2') 

は、私はsqliteの、postgresのではないテストしていますが、これらの二つの文は同じ出力を取得しているSQLにselect * from table1 cross join table2

を生成します特定のおよびその他の複雑なカスタムロジック。

これを試してください(これは、所望の生成する出力):

knex 
    .select('*') 
    .from(knex.raw('table1, table2')) 
0

一つは、常にデータベース - のためknex.rawを使用することができます。

関連する問題