如何根据第二个 table 的值更新一个 table 中的列?
How to update a column in one table based on values from a second table?
给定 PostgreSQL 数据库中的以下两个表...
Table observation
:
+---------------+---------+
| observationid | deleted |
+---------------+---------+
| 1 | F |
| 2 | F |
| 3 | F |
+---------------+---------+
Table numericvalue
:
+---------------+-------+
| observationid | value |
+---------------+-------+
| 1 | 999 |
| 2 | 5 |
| 3 | 999 |
+---------------+-------+
只要 numericvalue
中的关联行包含 value
,我想将 observation
中所有行的 deleted
列的值设置为 'T'
等于 999
.
SELECT
查询如下所示:
SELECT *
FROM observation o
INNER JOIN numericvalue n
ON o.observationid = n.observationid
AND n.value = 999;
UPDATE observation ob
SET deleted = 'T'
FROM numericvalue nv
WHERE nv.value = 999
AND nv.observationid = ob.observationid;
UPDATE observation
SET deleted = 'T'
WHERE observationid IN (SELECT observationid FROM numericvalue WHERE value=999)
给定 PostgreSQL 数据库中的以下两个表...
Table observation
:
+---------------+---------+
| observationid | deleted |
+---------------+---------+
| 1 | F |
| 2 | F |
| 3 | F |
+---------------+---------+
Table numericvalue
:
+---------------+-------+
| observationid | value |
+---------------+-------+
| 1 | 999 |
| 2 | 5 |
| 3 | 999 |
+---------------+-------+
只要 numericvalue
中的关联行包含 value
,我想将 observation
中所有行的 deleted
列的值设置为 'T'
等于 999
.
SELECT
查询如下所示:
SELECT *
FROM observation o
INNER JOIN numericvalue n
ON o.observationid = n.observationid
AND n.value = 999;
UPDATE observation ob
SET deleted = 'T'
FROM numericvalue nv
WHERE nv.value = 999
AND nv.observationid = ob.observationid;
UPDATE observation
SET deleted = 'T'
WHERE observationid IN (SELECT observationid FROM numericvalue WHERE value=999)