更新子表

时间:2012-03-22 02:45:26

标签: sql postgresql

假设我有一个名为result的子表。

SELECT school, grade 
  FROM (SELECT school, grade 
          FROM simulated_records 
         LIMIT 10) as result

我如何更新此子表的列?我试过了:

UPDATE result 
   SET grade = 'A' 

...但我收到了错误。

1 个答案:

答案 0 :(得分:2)

sub *选择*是临时的,它们无法更新。听起来你想要一个temporary table

CREATE TEMP TABLE temp_grades AS
SELECT school, grade FROM simulated_records;
UPDATE temp_grades SET grade = 'A';
编辑:你的评论:

UPDATE simulated_records
FROM (SELECT id FROM simulated_records WHERE school='Yale' LIMIT 10) AS result
SET grade='A'
WHERE id = result.id
RETURNING *;

以上使用子选择

的UPDATE FROM表操作

编辑2:第二评论:

假设你没有拼写错误,也许你有旧版本。还有另一种方法可以提供更好的支持:

UPDATE simulated_records SET grade = 'A' WHERE id IN
  (SELECT id FROM simulated_records WHERE school = 'Yale' LIMIT 10);