2017-01-20 51 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);