Postgres(错误:列“ date_deadline”的类型为date,但表达式的类型为text)

时间:2018-10-17 19:11:18

标签: postgresql

update res_user set date_of_birth= '1991-07-30' where name = 'USER1';

这项工作,但只能更新一行。

在我的情况下,我想在同一查询中用不同的数据更新多行,所以我这样写,不幸的是,它行不通。

查询:

update res_user as ru set
date_of_birth = n.date_of_birth
from (values
    ('1991-07-30','User1'),
    ('1980-06-30','User2'),
    ('1975-02-12','User3'),
) as n(date_of_birth, name)
where n.name = ru.name;

结果:

ERROR:  column "date_of_birth" is of type date but expression is of 
type text
LINE 2:     date_of_birth = n.date_of_birth
                        ^
HINT:  You will need to rewrite or cast the expression.

1 个答案:

答案 0 :(得分:0)

您需要投射:

update res_user as ru set
date_of_birth = n.date_of_birth
from (values
    ('1991-07-30'::date,'User1'),
    ('1980-06-30'::date,'User2'),
    ('1975-02-12'::date,'User3'),
) as n(date_of_birth, name)
where n.name = ru.name;