微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Postgres拒绝将时间戳值从一个表插入到另一个表

如何解决Postgres拒绝将时间戳值从一个表插入到另一个表

表foo:

create table table_foo
(
    foo_id            integer,foo_some_timestamp timestamp without timezone
)

表格栏:

create table table_bar
(
    bar_id            integer,foo_id            integer,bar_some_timestamp timestamp without timezone
)

当我这样插入时,它会失败:

insert into table_bar (foo_id,bar_some_timestamp) (
  select foo_id,foo_some_timestamp as bar_some_timestamp
  from foo
  left join table_bar on table_foo.foo_id = table_bar.foo_id
);

但是我得到一个错误

column "foo_sime_timestamp" is of type timestamp without time zone but expression is of type text Hint: You will need to rewrite or cast the expression.

我尝试过to_timestamp,但奇怪地引发了以下错误

to_timestamp(foo_some_timestamp,'YYYY-MM-DD HH24-MI-SS.US')::timestamp without time zone as bar_some_timestamp

 ERROR: function to_timestamp(timestamp without time zone,unkNown) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.

由于是左联接,因此结果包含很多空值。但是使用COALESCE会产生相同的错误

我想念什么? (Postgres v11)

解决方法

您的目标表有三列,但您的选择仅提供了两列。您应该始终对INSERT语句中的目标列进行限定,但是如果所提供的内容少于目标表所提供的内容,则这是强制性的。这些列是按位置而不是名称匹配的。

insert into table_bar (foo_id,bar_some_timestamp)
select tf.foo_id,tf.foo_some_timestamp
from table_foo tf
  left join table_bar tb on tf.foo_id = tb.foo_id
;

Online example

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。