如何根据第二个表中的值更新一个表中的列?

时间:2015-11-10 22:36:13

标签: postgresql sql-update sql-subselect

在PostgreSQL数据库中给出以下两个表...

observation

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

numericvalue

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

每当deleted中的关联行包含'T'等于observation时,我想在numericvalue的所有行中将列value的值设置为999 SELECT

SELECT * FROM observation o INNER JOIN numericvalue n ON o.observationid = n.observationid AND n.value = 999; 查询如下所示:

import re

some_var = "what what"

should_match = "what what what what what what hey"
not_a_match = "what what what what hey what what"

match = re.search(re.escape(some_var){3}, should_match)
no_match = re.search(re.escape(some_var){3}, not_a_match)

2 个答案:

答案 0 :(得分:1)

UPDATE observation ob
SET deleted = 'T'
FROM numericvalue nv
WHERE nv.value = 999
AND nv.observationid = ob.observationid;

答案 1 :(得分:0)

UPDATE observation
SET deleted = 'T'
WHERE observationid IN (SELECT observationid FROM numericvalue WHERE value=999)