如何使用带有select语句的where子句执行更新语句

时间:2017-01-20 09:48:16

标签: postgresql

我在postgres 9.5中有一个表(学生),其中包含以下模式:

  id          |   marks     |    division   | class
   1                90              A          1
   2                90              B          2
   3                90              B          1

我想更新学生的班级1和标志= 90到" A"。

我知道我可以简单地使用update student set division='A' where class =1 and marks=90

但是要了解如何使用select语句在查询中返回多行。 Somethig喜欢:

update student set division ='A' where id=(select id from student where class=1 and marks=90)

我是postgres的新手。一些指示将有所帮助。

1 个答案:

答案 0 :(得分:0)

您不需要子选择:

update student
   set division = 'A'
where class = 1 
  and marks = 90;

但如果您坚持使用子选择,则需要IN

update student
   set division = 'A'
where id in (select id 
             from student 
             where class = 1 
               and marks = 90);
相关问题