如果我理解正确,只需在SELECT语句中添加两个子查询来查找正确的ID。例如:
SELECT id,
(SELECT correct_id FROM User WHERE import_id=user_id) AS UserID,
(SELECT correct_id FROM Item WHERE import_id=item_id) AS ItemID,
created_at,
updated_at
FROM Downloads
这将你的不正确翻译user_ids您要来自用户表的任何标识,它会做同样的为您ITEM_IDS。来自SQL的信息现在是正确的。
但是,如果你想更新用正确的信息的表,你可以写这就像这样:
UPDATE Downloads
SET user_id = User.user_id,
item_id = Item.item_id
FROM Downloads
INNER JOIN User ON Downloads.user_id = User.import_id
INNER JOIN Item ON Downloads.item_id = Item.import_id
WHERE ...
确保装上去在WHERE子句中,所以你不更新每个记录在下载表中(除非这是计划)。我重写了上面的语句,因为原始版本每行有两个SELECT语句,这有点激烈。
编辑: 由于这是PostgreSQL的,你不能在UPDATE
和FROM
部分都具有表名。相反,FROM
部分中的表连接到正在更新的表。下面是从PostgreSQL的网站关于这个报价:
When a FROM clause is present, what essentially happens is that the target table is joined to the tables mentioned in the fromlist, and each output row of the join represents an update operation for the target table. When using FROM you should ensure that the join produces at most one output row for each row to be modified. In other words, a target row shouldn't join to more than one row from the other table(s). If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable.
http://www.postgresql.org/docs/8.1/static/sql-update.html
考虑到这一点,这里是我认为应该工作的例子(不能测试它,不好意思):
UPDATE Downloads
SET user_id = User.user_id,
item_id = Item.item_id
FROM User, Item
WHERE Downloads.user_id = User.import_id AND
Downloads.item_id = Item.import_id
那是基本的想法。不要忘记,您仍然需要为WHERE
部分添加额外条件来限制更新的行。
谢谢!我需要用正确的值更新下载表。 UPDATE文件在哪里? (是的,我用SQL很糟糕) – jmccartie 2011-05-14 16:17:51
@jmccartie - 你去了。如果你只是想要正确的数据,第一个SQL语句就可以工作。如果你想更新数据,第二条语句将完成这项工作。如果你想做这两个,更新数据(第二条语句),然后只需使用简单的SELECT语句从Downloads表中获取数据。 – IAmTimCorey 2011-05-14 16:28:26
嗯。我得到一个新的查询错误:错误:错误:表名“下载”指定多次。有任何想法吗? – jmccartie 2011-05-15 00:07:03