2015-11-10 102 views
0

鑑於PostgreSQL數據庫下面兩個表...如何根據第二個表中的值更新一個表中的列?

observation

+---------------+---------+ 
| observationid | deleted | 
+---------------+---------+ 
|  1  | F | 
|  2  | F | 
|  3  | F | 
+---------------+---------+ 

numericvalue

+---------------+-------+ 
| observationid | value | 
+---------------+-------+ 
|  1  | 999 | 
|  2  | 5 | 
|  3  | 999 | 
+---------------+-------+ 

我想deleted列的值設置爲'T'只要numericvalue中的關聯行包含value等於的所有行在。

SELECT查詢看起來是這樣的:

SELECT * 
FROM observation o 
INNER JOIN numericvalue n 
ON o.observationid = n.observationid 
AND n.value = 999; 

回答

1
UPDATE observation ob 
SET deleted = 'T' 
FROM numericvalue nv 
WHERE nv.value = 999 
AND nv.observationid = ob.observationid; 
0
UPDATE observation 
SET deleted = 'T' 
WHERE observationid IN (SELECT observationid FROM numericvalue WHERE value=999) 
相關問題