gpt4 book ai didi

PostgreSQL - 绑定(bind)变量和日期添加

转载 作者:行者123 更新时间:2023-11-29 11:21:28 29 4
gpt4 key购买 nike

我需要更新 Postgres (8.3) 数据库表中的一些时间戳列。

我的查询(简化)如下所示:

update table1 set dateA = dateA + interval '10 hours' where id = 1234;

这是脚本的一部分,有很多内容需要更新,所以我更喜欢使用绑定(bind)变量,而不是每次都必须构建查询字符串。这意味着我的查询变为:

update table1 set dateA = dateA + interval '? hours' where id = ?;

当我这样做时,提示是我提供了 2 个绑定(bind)变量,而实际上只需要一个。

如果我尝试将 ? 放在引号之外:

update table1 set dateA = dateA + interval ? ' hours' where id = ?;

我得到:

... syntax error at or near "' hours'"

看起来好像查询已被解释为

... dateA = dateA + interval '10' ' hours' ...

我在文档中找不到任何帮助...有什么建议吗?

谢谢

最佳答案

试试这个:

update table1 set dateA = dateA + ( (interval '1 hours') * ? ) where id = ?;

或者这个:

update table1 set dateA = dateA + cast(? || ' hours' as interval) where id = ?;

关于PostgreSQL - 绑定(bind)变量和日期添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808385/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com