Node.js 数据表编辑器 - 如何使用构成连接的 (knex) VIEW
Node.js datatables editor - How to use a (knex) VIEW that composes a join
我正在使用 node.js 数据表编辑器库。当尝试进行复杂的左连接时 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' ...像这样:
let editor = new Editor( db, 'portfolios_isin_mm' )
.fields(
new Field( 'portfolios_isin_mm.account_id' ),
new Field( 'portfolios_isin_mm.user_id' ),
new Field( 'portfolios_isin_mm.uid_foreign' )
new Field( 'securities.issuer_name' ),
new Field( 'portfolios_isin_mm.isin' ),
new Field( 'portfolios_isin_mm.mic' ),
)
.leftJoin( 'portfolios_isin', 'portfolios_isin.id', '=', 'portfolios_isin_mm.uid_foreign' )
.leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' );
//.leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin' );
我遇到了一个错误。调试错误显示我:
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`mic)`' at line 1
at Query.Sequence._packetToError (/home/myproject/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/myproject/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/home/myproject/node_modules/mysql/lib/protocol/Protocol.js:278:23)
at Parser.write (/home/myproject/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/myproject/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/myproject/node_modules/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/home/myproject/node_modules/mysql/lib/Connection.js:502:10)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
--------------------
at Protocol._enqueue (/home/myproject/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/home/myproject/node_modules/mysql/lib/Connection.js:200:25)
at /home/myproject/node_modules/knex/lib/dialects/mysql/index.js:152:18
at Promise._execute (/home/myproject/node_modules/bluebird/js/release/debuggability.js:313:9)
at Promise._resolveFromExecutor (/home/myproject/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/home/myproject/node_modules/bluebird/js/release/promise.js:79:10)
at Client_MySQL._query (/home/myproject/node_modules/knex/lib/dialects/mysql/index.js:146:12)
at Client_MySQL.query (/home/myproject/node_modules/knex/lib/client.js:197:17)
at Runner.<anonymous> (/home/myproject/node_modules/knex/lib/runner.js:146:36)
at Runner.tryCatcher (/home/myproject/node_modules/bluebird/js/release/util.js:16:23)
at Runner.query (/home/myproject/node_modules/bluebird/js/release/method.js:15:34)
at /home/myproject/node_modules/knex/lib/runner.js:65:21
at tryCatcher (/home/myproject/node_modules/bluebird/js/release/util.js:16:23)
at /home/myproject/node_modules/bluebird/js/release/using.js:185:26
at tryCatcher (/home/myproject/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/myproject/node_modules/bluebird/js/release/promise.js:512:31)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'.`mic)`\' at line 1',
sqlState: '42000',
index: 0,
sql:
'select count(`portfolios_isin_mm`.`id`) as `cnt` from `portfolios_isin_mm` left join `portfolios_isin` on `portfolios_isin`.`id` = `portfolios_isin_mm`.`uid_foreign` left join `securities` on `securities`.`isin` = `portfolios_isin_mm`.`isin AND (securities`.`mic = portfolios_isin_mm`.`mic)`' }
...使用 php 库它工作得很好:
->leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' )
据说我可以引用'The only way to do this at the moment is to create a VIEW that composes that join then you can select from that view. The readTable (https://editor.datatables.net/docs/1.8.1/nodejs/classes/editor.editor.html#readtable) method can be used to read information from the view while still updating to the host table.'
我怎样才能让复杂的左连接在 node.js 中工作(使用构成该连接的 VIEW(使用 knex))?
...我正在使用 $ node -v ...v10.15.0 和 "datatables.net-editor-server": "^1.8.1"
你在最后一个 leftjoin 中有一个语法错误。
.leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' )
这就是生成错误 SQL 查询的原因:
select count(`portfolios_isin_mm`.`id`) as `cnt` from `portfolios_isin_mm` left join `portfolios_isin` on `portfolios_isin`.`id` = `portfolios_isin_mm`.`uid_foreign` left join `securities` on `securities`.`isin` = `portfolios_isin_mm`.`isin AND (securities`.`mic = portfolios_isin_mm`.`mic)`'
注意从以下开始的不完整引号:portfolios_isin_mm
.`isin
您应该考虑使用 andOn() 在查询中包含 'AND':
.leftJoin('securities', function() {
this.on('securities.isin', '=', 'portfolios_isin_mm.isin')
.andOn('securities.mic', '=', portfolios_isin_mm.mic')
})
我正在使用 node.js 数据表编辑器库。当尝试进行复杂的左连接时 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' ...像这样:
let editor = new Editor( db, 'portfolios_isin_mm' )
.fields(
new Field( 'portfolios_isin_mm.account_id' ),
new Field( 'portfolios_isin_mm.user_id' ),
new Field( 'portfolios_isin_mm.uid_foreign' )
new Field( 'securities.issuer_name' ),
new Field( 'portfolios_isin_mm.isin' ),
new Field( 'portfolios_isin_mm.mic' ),
)
.leftJoin( 'portfolios_isin', 'portfolios_isin.id', '=', 'portfolios_isin_mm.uid_foreign' )
.leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' );
//.leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin' );
我遇到了一个错误。调试错误显示我:
{ Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`mic)`' at line 1
at Query.Sequence._packetToError (/home/myproject/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/home/myproject/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/home/myproject/node_modules/mysql/lib/protocol/Protocol.js:278:23)
at Parser.write (/home/myproject/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/myproject/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/home/myproject/node_modules/mysql/lib/Connection.js:91:28)
at Socket.<anonymous> (/home/myproject/node_modules/mysql/lib/Connection.js:502:10)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
--------------------
at Protocol._enqueue (/home/myproject/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/home/myproject/node_modules/mysql/lib/Connection.js:200:25)
at /home/myproject/node_modules/knex/lib/dialects/mysql/index.js:152:18
at Promise._execute (/home/myproject/node_modules/bluebird/js/release/debuggability.js:313:9)
at Promise._resolveFromExecutor (/home/myproject/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/home/myproject/node_modules/bluebird/js/release/promise.js:79:10)
at Client_MySQL._query (/home/myproject/node_modules/knex/lib/dialects/mysql/index.js:146:12)
at Client_MySQL.query (/home/myproject/node_modules/knex/lib/client.js:197:17)
at Runner.<anonymous> (/home/myproject/node_modules/knex/lib/runner.js:146:36)
at Runner.tryCatcher (/home/myproject/node_modules/bluebird/js/release/util.js:16:23)
at Runner.query (/home/myproject/node_modules/bluebird/js/release/method.js:15:34)
at /home/myproject/node_modules/knex/lib/runner.js:65:21
at tryCatcher (/home/myproject/node_modules/bluebird/js/release/util.js:16:23)
at /home/myproject/node_modules/bluebird/js/release/using.js:185:26
at tryCatcher (/home/myproject/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/myproject/node_modules/bluebird/js/release/promise.js:512:31)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage:
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'.`mic)`\' at line 1',
sqlState: '42000',
index: 0,
sql:
'select count(`portfolios_isin_mm`.`id`) as `cnt` from `portfolios_isin_mm` left join `portfolios_isin` on `portfolios_isin`.`id` = `portfolios_isin_mm`.`uid_foreign` left join `securities` on `securities`.`isin` = `portfolios_isin_mm`.`isin AND (securities`.`mic = portfolios_isin_mm`.`mic)`' }
...使用 php 库它工作得很好:
->leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' )
据说我可以引用'The only way to do this at the moment is to create a VIEW that composes that join then you can select from that view. The readTable (https://editor.datatables.net/docs/1.8.1/nodejs/classes/editor.editor.html#readtable) method can be used to read information from the view while still updating to the host table.'
我怎样才能让复杂的左连接在 node.js 中工作(使用构成该连接的 VIEW(使用 knex))?
...我正在使用 $ node -v ...v10.15.0 和 "datatables.net-editor-server": "^1.8.1"
你在最后一个 leftjoin 中有一个语法错误。
.leftJoin( 'securities', 'securities.isin', '=', 'portfolios_isin_mm.isin AND (securities.mic = portfolios_isin_mm.mic)' )
这就是生成错误 SQL 查询的原因:
select count(`portfolios_isin_mm`.`id`) as `cnt` from `portfolios_isin_mm` left join `portfolios_isin` on `portfolios_isin`.`id` = `portfolios_isin_mm`.`uid_foreign` left join `securities` on `securities`.`isin` = `portfolios_isin_mm`.`isin AND (securities`.`mic = portfolios_isin_mm`.`mic)`'
注意从以下开始的不完整引号:portfolios_isin_mm
.`isin
您应该考虑使用 andOn() 在查询中包含 'AND':
.leftJoin('securities', function() {
this.on('securities.isin', '=', 'portfolios_isin_mm.isin')
.andOn('securities.mic', '=', portfolios_isin_mm.mic')
})