简体   繁体   English

"错误:错误:多次指定表名"

[英]Error : ERROR: table name specified more than once

I have a table "queued_items".我有一个表“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当前的“user_id”和“item_id”不正确,但存储在其他表中:users.imported_id 和 items.imported_id

Trying to grab the imported_id from the other tables and update.试图从其他表中获取imported_id 并更新。 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:尝试删除 FROM 行,收到此错误:

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我还尝试为 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

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) (postgres 9)

PS Trying to avoid this sub-query: 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)

...because it's crazy slow ...因为它太慢了

Try this: 尝试这个:

UPDATE queued_items
SET user_id = users.id,
    item_id = items.id
FROM users, items
WHERE queued_items.user_id = users.imported_id
  AND queued_items.item_id = items.imported_id

Yeah, old school join conditions. 是的,老学校加入条件。

From postgres site 来自postgres网站

UPDATE [ ONLY ] table [ [ AS ] alias ]
    SET { column = { expression | DEFAULT } |
          ( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
    [ FROM from_list ]
    [ WHERE condition | WHERE CURRENT OF cursor_name ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

*from_list* * from_list *

A list of table expressions, allowing columns from other tables to appear in the WHERE condition and the update expressions. 表表达式列表,允许其他表中的列出现在WHERE条件和更新表达式中。 This is similar to the list of tables that can be specified in the FROM Clause of a SELECT statement. 这类似于可以在SELECT语句的FROM子句中指定的表列表。 Note that the target table must not appear in the from_list, unless you intend a self-join (in which case it must appear with an alias in the from_list). 请注意,目标表不得出现在from_list中,除非您打算进行自联接(在这种情况下,它必须在from_list中显示别名)。

UPDATE queued_items
SET user_id = users.id,
    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

使用子查询语句并将索引添加到这些列。

I had to play around with the column/table naming and eventually got it to work. 我必须使用列/表命名并最终使其工作。 I had to: 我不得不:

  • leave out the table name in the destination SET columns 在目的地SET列中省略表名
  • ensure that I aliased the table being updated 确保我对正在更新的表别名

Your equivalent would be: 你的等价物是:

UPDATE queued_items
SET user_id = users.id,
    item_id = items.id
FROM queued_items as alias_queued_items
INNER JOIN users ON alias_queued_items.user_id = users.imported_id
INNER JOIN items ON alias_queued_items.item_id = items.imported_id

instead of: 代替:

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

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 in relations .就我而言,发生的事情是我eager loaded数据并将其指定为要加入relations的数据。

You should be able to change the name after the UPDATE to the alias. 您应该能够在UPDATE之后将名称更改为别名。 Also you can use the aliased names in the set clause. 您还可以在set子句中使用别名。 This means that you can set them in your JOIN clauses as well. 这意味着您也可以在JOIN子句中设置它们。

UPDATE qi 
SET qi.user_id = us.id,
    qi.item_id = itms.id
FROM queued_items qi
INNER JOIN users us ON qi.user_id = us.imported_id
INNER JOIN items itms ON qi.item_id = itms.imported_id

You don't need the FROM clause. 您不需要FROM子句。 Remove "FROM queued_items" and you are done. 删除“FROM queued_items”,您就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM