【问题标题】:Inserting date into PostgreSQL: Columns from and to [duplicate]将日期插入 PostgreSQL:从和到的列 [重复]
【发布时间】:2017-03-13 15:41:33
【问题描述】:

我无法将 date 插入 PostgreSQL 9.6 表中。

这是插入查询:

INSERT INTO rates (idproperty, from, to, price)
VALUES (1, '2017-03-14', '2017-04-30', 130);

结果就是这样:

ERROR:  syntax error at or near "from"
LINE 1: INSERT INTO rates (idproperty, from, to, price)

fromto 列使用数据类型 date 定义。

谢谢

【问题讨论】:

  • fromto 是保留关键字。
  • @LaurenzAlbe 是同一个问题,但保留关键字不同。谢谢你的帮助。

标签: database postgresql


【解决方案1】:

我相信您已经使用了 postgresql 保留字 - fromto 来创建您的表。为了在您的查询中使用它们,它们需要用引号括起来 "

试试这个方法:

INSERT INTO rates (idproperty, "from", "to", price)
VALUES (1, '2017-03-14', '2017-04-30', 130);

【讨论】:

    【解决方案2】:

    fromto 是 PostgreSQL 中的保留字。您需要使用" 来逃避它们:

    -- This fails:
    
    test=# SELECT from FROM rates;
    ERROR:  syntax error at or near "FROM"
    LINE 1: SELECT from FROM rates;
                        ^
    
    -- This works:
    
    test=# SELECT "from" FROM rates;
     from 
    ------
    (0 rows)
    

    查看保留字列表:https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html

    【讨论】:

    • 非常感谢,我快疯了 ;-)
    【解决方案3】:

    from 和 to 不是正确的保留字段,例如请更改为 from_i 和 to_i

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 2015-02-20
      • 2018-10-27
      • 2020-07-05
      • 2016-11-09
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多