错误:错误:多次指定表名
我有一个表“queued_items”。当前的“user_id”和“item_id”不正确,但存储在其他表中:users.imported_id和items.imported_id
尝试从其他表中获取imported_id并更新。这是我尝试的方法
UPDATE queued_items
SET queued_items.user_id = users.id,
queued_items.item_id = items.id
FROM queued_items
INNER JOIN users ON queued_items.user_id = users.imported_id
INNER JOIN items ON queued_items.item_id = items.imported_id
出现此错误:
Error : ERROR: table name "queued_items" specified more than once
尝试删除 FROM 行,出现此错误:
Error : ERROR: syntax error at or near "INNER"
LINE 4: INNER JOIN users ON queued_items.user_id = users.imported_id
^
我还尝试向 FROM 和 JOIN 条件添加别名
UPDATE queued_items
SET queued_items.user_id = users.id,
queued_items.item_id = items.id
FROM queued_items as qi
INNER JOIN users ON qi.user_id = users.imported_id
INNER JOIN items ON qi.item_id = items.imported_id
出现此错误:
Error : ERROR: column "queued_items" of relation "queued_items" does not exist
LINE 2: SET queued_items.user_id = users.id,
^
有什么想法吗? (postgres 9)
PS 试图避免这个子查询:
UPDATE queued_items
SET user_id = (SELECT id FROM users WHERE queued_items.user_id = users.imported_id),
item_id = (SELECT id FROM items WHERE queued_items.item_id = items.imported_id)
...因为它太慢了
I have a table "queued_items". The current "user_id" and "item_id" are incorrect, but are stored in the other tables: users.imported_id and items.imported_id
Trying to grab the imported_id from the other tables and update. Here's what I tried
UPDATE queued_items
SET queued_items.user_id = users.id,
queued_items.item_id = items.id
FROM queued_items
INNER JOIN users ON queued_items.user_id = users.imported_id
INNER JOIN items ON queued_items.item_id = items.imported_id
Getting this error:
Error : ERROR: table name "queued_items" specified more than once
Tried removing the FROM line, got this error:
Error : ERROR: syntax error at or near "INNER"
LINE 4: INNER JOIN users ON queued_items.user_id = users.imported_id
^
I also tried adding an alias to the FROM and JOIN conditions
UPDATE queued_items
SET queued_items.user_id = users.id,
queued_items.item_id = items.id
FROM queued_items as qi
INNER JOIN users ON qi.user_id = users.imported_id
INNER JOIN items ON qi.item_id = items.imported_id
Got this error:
Error : ERROR: column "queued_items" of relation "queued_items" does not exist
LINE 2: SET queued_items.user_id = users.id,
^
Any ideas? (postgres 9)
PS Trying to avoid this sub-query:
UPDATE queued_items
SET user_id = (SELECT id FROM users WHERE queued_items.user_id = users.imported_id),
item_id = (SELECT id FROM items WHERE queued_items.item_id = items.imported_id)
...because it's crazy slow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个:
是的,老派的加入条件。
Try this:
Yeah, old school join conditions.
来自 postgres 站点
*from_list*
From postgres site
*from_list*
我不得不尝试列/表命名并最终让它工作。我必须:
SET
列中省略表名称,您的等效项是:
而不是:
I had to play around with the column/table naming and eventually got it to work. I had to:
SET
columnsYour equivalent would be:
instead of:
使用子查询语句并为这些列添加索引。
Use the sub-query statement and add indexes to those columns.
不知道这是否有帮助。就我而言,发生的情况是我
急切地加载
数据,并将其指定为要加入关系
的数据。Don't know if this helps. In my case, what happened was that I
eager loaded
the data and also specified it as data to be joined inrelations
.您应该能够将
UPDATE
之后的名称更改为别名。您还可以在 set 子句中使用别名。这意味着您也可以在JOIN
子句中设置它们。You should be able to change the name after the
UPDATE
to the alias. Also you can use the aliased names in the set clause. This means that you can set them in yourJOIN
clauses as well.您不需要 FROM 子句。删除“FROM queued_items”即可完成。
You don't need the FROM clause. Remove "FROM queued_items" and you are done.