2017-01-20 50 views
1

我有一个表(学生)与Postgres的9.5以下架构的更新语句:如何执行与where子句为select语句

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)

我是新来的Pos​​tgres。一些指针会有所帮助。

+2

'WHERE ID IN(SELECT ID。 ...)' – AlexM

回答

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);