gpt4 book ai didi

javascript - Node.js 数据表编辑器 - 如何使用组成联接的 (knex) VIEW

转载 作者:行者123 更新时间:2023-12-03 00:27:42 25 4
gpt4 key购买 nike

我正在使用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)' ) 

据说我可以引用'目前执行此操作的唯一方法是创建一个组成该连接的 View ,然后您可以从该 View 中进行选择。 readTable ( https://editor.datatables.net/docs/1.8.1/nodejs/classes/editor.editor.html#readtable ) 方法可用于从 View 读取信息,同时仍更新到主机表。'

如何让复杂的左连接也能在 node.js 中工作(使用组成该连接的 VIEW(使用 knex))?

...我正在使用 $ node -v ... v10.15.0 和 "datatables.net-editor-server": "^1.8.1"

最佳答案

最后一个左连接中有语法错误。

.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')
})

关于javascript - Node.js 数据表编辑器 - 如何使用组成联接的 (knex) VIEW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54021010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com