gpt4 book ai didi

sql - PSQL 更新语法——使用表的循环引用

转载 作者:行者123 更新时间:2023-11-29 13:00:21 25 4
gpt4 key购买 nike

这是我要编写的 PSQL 命令:

UPDATE accounts
SET account_type_id=subquery.id
FROM (SELECT id
FROM account_types
WHERE mongo_id = accounts.mongo_account_type_id) AS subquery
WHERE accounts.id = 1;

产生此错误消息:

ERROR: invalid reference to FROM-clause entry for table "accounts" LINE 1: ...OM (SELECT id FROM account_types WHERE mongo_id = accounts.m... ^ HINT: There is an entry for table "accounts", but it cannot be referenced from this part of the query.

数据结构的简短解释——从 MongoDB 迁移到 PSQL,我已经将所有记录移到 PSQL 中,维护旧的 Mongo BSONs 以引用和重新缝合具有外键关系的记录——我的账户表有如下列:

mongo_id                       | 521289ae1e0345000200000f
mongo_merchant_id | 55e13f0afbab68982e000006
mongo_account_type_id | 521289c41e03450002000011
mongo_admin_id | 518b762e89651a0389000013

我的 account_types 表有:

mongo_id          | 521289c41e03450002000011
mongo_merchant_id | 55e13f0afbab68982e000006

因此,我尝试使用 accounts.mongo_account_type_id == account_types.mongo_id 作为引用,将我的 ACCOUNTS 表的 account_type_id 字段设置为 ACCOUNT_TYPES 表中 id 字段的 INTEGER 值。

最终,帐户应该有 account_type_id = 1(在这种情况下 - 这种特殊情况下,记录是 account_type 的 id 1)

在我的查询中,我只是对如何使用 accounts 表作为 mongo_account_type_id 的“变量”进行子查询感到困惑。

最佳答案

据我了解您的意图,查询应如下所示:

UPDATE accounts a
SET account_type_id = t.id
FROM account_types t
WHERE t.mongo_id = a.mongo_account_type_id
AND a.id = 1;

关于sql - PSQL 更新语法——使用表的循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288425/

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