表A:
id | dob
表B:
id | type
我想根据A.dob来计算年龄,并基于此来更新B.type,我尝试了以下方法,但这给了我一个错误。
UPDATE B
SET B.type = CASE
WHEN AGE <= 16 THEN 'C'
WHEN AGE>25 and age<=40 THEN 'Y'
WHEN AGE>40 THEN 'O'
END
from AGE as ( EXTRACT(YEAR FROM age(now(),A.dob)) ), A inner join B on A.id=B.id
where A.dob is not null;
答案 0 :(得分:0)
尝试这样的事情:
update b
set type = (case when age <= 16 then 'C'
when age > 25 and age <= 40 then 'Y'
when age > 40 then 'O'
end)
from (select a.*, extact(year from age(now(), a.dob)) as age
from a
) a
where a.id = b.id and a.age is not null;
注意:
FROM
子句中正在更新的表。那不是Postgres的工作方式。JOIN
条件---进入WHERE
子句。age
。age
不是NULL
而不是基础列。